SQL CASE and local variables

sql, sql-server, sql-server-2008, t-sql

Solution

Two ways to use CASE in this scenario with MSSQL

DECLARE 
    @test   int,
    @result char(10)

SET @test = 10

SET @result = CASE @test
    WHEN 10 THEN 
        'OK test'
    ELSE
        'Test is not OK'
END

PRINT @result;

SET @result = CASE 
    WHEN @test = 10 THEN 
        'OK test'
    ELSE
        'Test is not OK'
END

PRINT @result

Problem

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; DECLARE @Result char(10); SET @Test = 10; CASE @Test WHEN @Test = 10 THEN SET @Result='OK test' END Print @Result; ``` I use MS SQL 2008.

Original source