The Case expression syntax is:
CASE ..... WHEN ....... THEN ........ [ ...more WHEN ... THEN expressions ]
[ ELSE ......... ] END
ELSE is not obligatory expression.
And - If this argument is omitted and no comparison operation evaluates to TRUE, CASE returns NULL.
CASE without the Else expression can cause to insertion of NULL values!
For Example:
DECLARE @TestTable (TestColumn int not null)
INSERT INTO @TestTable select 1 union select 2 union select 3
-- case without else expression
UPDATE@TestTable
SET TestColumn = ( CASE WHEN TestColumn = 2 THEN 22 END )
-- Result: table values are: NULL, 22, NULL
-- if we want to update only the '2' rows:
UPDATE@TestTable
SET TestColumn = ( CASE WHEN TestColumn = 2 THEN 22 ELSE TestColumn END )
No comments:
Post a Comment