Question : Update based on Case statements

Hello All,

I have this query which uses IF EXISTS and even if a single record found it woudl raise error but now I have to update a table with boolean values based on record returns. If record exist then true else false.

Can some one help me on how to update query for this?

IF EXISTS (
      SELECT 1
        FROM (
             SELECT MAX(CleanDate) AS MaxCleanDate
                    FROM #table a
                    INNER JOIN T#table1 b
                              ON a.CodeID = b.CodeID
                              AND a.Subcode = b.Subcode
                              AND a.TranID = b.TranID
                    WHERE a.Code = @CodeID  
                         AND a.Subcode = @Subcode
        ) AS MaxCleanDateEvent
       WHERE MaxCleanDate > DATEDIFF(hh, -24, GETDATE())
)

How to write update-
UPDATE #table1
SET Comflag = CASE WHEN.....

Thanks,
-B

Answer : Update based on Case statements

UPDATE #table1
SET Comflag = CASE WHEN
EXISTS (
      SELECT 1
        FROM (
             SELECT MAX(CleanDate) AS MaxCleanDate
                    FROM #table a
                    INNER JOIN T#table1 b
                              ON a.CodeID = b.CodeID
                              AND a.Subcode = b.Subcode
                              AND a.TranID = b.TranID
                    WHERE a.Code = @CodeID  
                         AND a.Subcode = @Subcode
        ) AS MaxCleanDateEvent
       WHERE MaxCleanDate > DATEDIFF(hh, -24, GETDATE())
) THEN 1 ELSE 0 END
Random Solutions  
 
programming4us programming4us