Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bracket for IF statement in Stored Procedure

I know this is such a silly question but I tried every bracket I can think of to wrap the IF statement but it seems that none of them work.

For example:

IF(@item!=0){

   //Do stub
   RETURN   // without the brakets , this return does not belong to if
 }

   //Do some stubs  // and if the condition of the IF is false, this statement can't be reached

Thank you

like image 215
Xitrum Avatar asked Aug 12 '11 11:08

Xitrum


2 Answers

try begin and end instead

IF(@item!=0)
begin
/*Do stub*/
RETURN   
/* without the brakets , this return does not belong to if  */
end 
like image 178
t-clausen.dk Avatar answered Oct 11 '22 07:10

t-clausen.dk


Instead of RETURN, you can also use ELSE:

if @item != 0
begin
   -- Do stub
end
else
begin
    -- Do some stubs  
    -- and if the condition of the IF is false, this statement can't be reached
end

Plus:

  • you don't need the brackets around the IF clause
  • you have to use -- instead of // for comments (see my example)
like image 43
Christian Specht Avatar answered Oct 11 '22 05:10

Christian Specht