Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Variable value in exists condition sql server

 Declare @CategoryID as int BEGIN       SELECT      (CASE          WHEN EXISTS(             SELECT t0.Categoryid AS [EMPTY]             FROM Categories AS [t0]             WHERE [t0].Categoryname = @CategoryName            ) THEN 1         ELSE 0      END) AS [value] 

if i want to set my variable inside exists block with t0.Categoryid how could i do that ?

what i want is to replace then 1 to the category id value...

thanks in advance..

like image 852
Vishal Sharma Avatar asked Mar 11 '14 09:03

Vishal Sharma


1 Answers

Declare @CategoryID as int SET @CategoryID =  CASE WHEN EXISTS(SELECT 1                                     FROM  Categories                                     WHERE Categoryname = @CategoryName)                      THEN 1 ELSE 0                    END 

Another way would be something like ....

IF EXISTS (SELECT 1            FROM  Categories            WHERE Categoryname = @CategoryName)  BEGIN    SET @CategoryID = 1;  END ELSE  BEGIN    SET @CategoryID = 0;  END 
like image 75
M.Ali Avatar answered Sep 18 '22 10:09

M.Ali