我有一个变量@tableset。这些应包含计数不为0的表。例如
if @count1 <> 0 then @tableset = @table1
else if @count1 = 0 and @count2 <> 0 then @tablset = @table2
else if @count1 = 0 and @count2 = 0 then @tableset = @table1 + @table2
where count1 is the count of table1
count2 is the count of table2
我不想编写多个if条件,而是希望以一种代码有效的方式实现这一点,以便将来在表数量增加的情况下,更容易扩展此查询。
这是我为SQL Server编写的示例代码:
IF @count1 = 0 and @count2 = 0 and @count3 = 0 and @count4 =0 and @count5 = 0
set @tableset=@table1
IF @count1 = 0 and @count2 = 0 and @count3 = 0 and @count4 =0 and @count5 = 0
set @tableset=@table1
因此,我需要使用以下逻辑编写存储的proc:
if @count1 <> 0 then @tableset = @table1
else if @count1 = 0 and @count2 <> 0 then @tablset = @table2
else if @count1 = 0 and @count2 = 0 then @tableset = @table1 + @table2