Harry
Harry

Reputation: 2941

SQL Server : IF THEN ELSE in stored procedure

I have a stored procedure that I pass an integer parameters to (@truncate). What I want is this.

if @truncate = 1
then 
   truncate some_table 
   do something else
else 
   do only the "do something else" (before the else) without truncating the table.. 

The "do something else" part of the code is quite long.. How do I do this without repeating the "do something else" code and making the stored procedure longer than necessary?

Upvotes: 0

Views: 2076

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521467

If I understand correctly, you only need an IF to cover the truncation, and the other logic should always execute. So just remove the ELSE condition and place that logic after the IF.

if @truncate = 1
begin 
    truncate some_table
end

do something else   -- always do this

Upvotes: 4

Related Questions