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

时间:2017-11-30 04:03:43

标签: sql-server tsql stored-procedures sql-server-2016

我有一个存储过程,我将整数参数传递给(@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.. 

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

1 个答案:

答案 0 :(得分:4)

如果我理解正确,你只需要IF来覆盖截断,而其他逻辑应该总是执行。因此,只需删除ELSE条件,并将该逻辑放在IF

之后
if @truncate = 1
begin 
    truncate some_table
end

do something else   -- always do this