DECLARE @tblYesRecords TABLE (
Id BIGINT
,Answer NVARCHAR(10)
)
DECLARE @tblNoRecords TABLE (
Id BIGINT
,Answer NVARCHAR(10)
)
INSERT INTO @tblYesRecords (
Id
,Answer
)
SELECT ad.Id
,ad.Answer
FROM dbo.AuditDetail ad
INNER JOIN dbo.QuestionMaster qm ON ad.QuestionId = qm.Id
AND qm.IsRedAlerts = 1
INNER JOIN dbo.Audit a ON ad.AuditId = a.Id
AND a.CreatedDate < '2017-03-22 17:00:00.000'
WHERE UPPER(ltrim(rtrim(ad.Answer))) = 'Y'
ORDER BY a.Id --185
INSERT INTO @tblNoRecords (
Id
,Answer
)
SELECT ad.Id
,ad.Answer
FROM dbo.AuditDetail ad
INNER JOIN dbo.QuestionMaster qm ON ad.QuestionId = qm.Id
AND qm.IsRedAlerts = 1
INNER JOIN dbo.Audit a ON ad.AuditId = a.Id
AND a.CreatedDate < '2017-03-22 17:00:00.000'
WHERE UPPER(ltrim(rtrim(ad.Answer))) = 'N'
ORDER BY a.Id --51849
---------------------------------Yes to No-------------------------------------
UPDATE ad
SET ad.Answer = 'N'
FROM dbo.AuditDetail ad
INNER JOIN @tblYesRecords tyr ON ad.Id = tyr.Id
---------------------------------No to Yes-------------------------------------
UPDATE ad
SET ad.Answer = 'Y'
FROM dbo.AuditDetail ad
INNER JOIN @tblNoRecords tyr ON ad.Id = tyr.Id