Updated on Kisan Patel
This tutorial will show you how to use CASE
statement to set a variable value in SQL Server?
DECLARE @cId varchar(55) SET @cId = 'ALFKI' DECLARE @isExists varchar(55) SET @isExists = CASE WHEN EXISTS (SELECT CustomerID FROM Customers WHERE CustomerID = @cId) THEN 'Customer detials exists.' WHEN EXISTS (SELECT Region FROM Customers WHERE CustomerID = @cId) THEN 'Customers Details Exists but Region do not' ELSE 'N/A' END; print @isExists
In above case, the value of @cId is ‘ALFKI’ that exists in the Customers table and the value of the
@isExists variable is set to ‘Customer detials exists’.