I would like to know how I can use local variables in CASE
statements in SQL?
This script gives me an error:
DECLARE @Test int;
DE
In SQL Server I would write it like this:
DECLARE @Test int;
DECLARE @Result char(10);
SET @Test = 10;
SET @Result = CASE @Test
WHEN 10
THEN 'OK test'
END
Print @Result;
The WHEN
clause does not have @Test = 10
, as the @Test
variable is stated in the CASE
clause.
See the CASE documentation for SQL Server.