SQL Server:如果那么在存储过程中

时间:2022-01-21 03:49:54

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

我有一个存储过程,我将整数参数传递给(@truncate)。我想要的是这个。

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?

代码中的“做其他事情”部分很长。如何在不重复“执行其他操作”代码的情况下执行此操作并使存储过程的时间超过必要时间?

1 个解决方案

#1


4  

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来覆盖截断,而另一个逻辑应该总是执行。因此,只需删除ELSE条件并将该逻辑置于IF之后。

if @truncate = 1
begin 
    truncate some_table
end

do something else   -- always do this

#1


4  

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来覆盖截断,而另一个逻辑应该总是执行。因此,只需删除ELSE条件并将该逻辑置于IF之后。

if @truncate = 1
begin 
    truncate some_table
end

do something else   -- always do this