Search This Blog

Thursday, June 20, 2013

SYBASE:How To use Case statement in UPDATE Clause

Below is the syntax to use case statement in UPDATE Clause:
create table #tmp1 (col1 INT NULL, col2 INT NULL)

INSERT INTO #tmp1 values (1,1)
INSERT INTO #tmp1 values (2,1)
INSERT INTO #tmp1 values (3,2)
INSERT INTO #tmp1 values (4,1)

UPDATE #tmp1
SET col1 = CASE WHEN t.col1 NOT IN (SELECT t2.col FROM #tmp2 t2)
                    THEN 9
                ELSE 5
           END
FROM  #tmp1 t
WHERE t.col2 = 1     
NOTE: Make sure to use END as the closing command for the CASE statement.

No comments:

Post a Comment