用另一个Execute @执行@

时间:2013-02-05 15:18:08

标签: sql sql-server execute

我该如何使这项工作?我希望能够更新@ result0,具体取决于变量是0还是1,然后将其放入另一个集合@然后调用@result。 ## tempz表确实存在。当我运行它时,它只是给了我:( 125行(s)受影响)

DECLARE @result nvarchar(max),@result0 nvarchar(max)
SET @showstore = 1
SET @showcashier = 0
SET @showregister = 0
SET @showdate = 0  

set @result = 'select '+@result0+' from ##tempz'
set @result0 = 'Amex,[Gift Card],Debit,[Off Line C.Card],[Str Cr],[House Acct],Cash,Rebate,[C.Card],VIP,Discover,[Check],MasterCard,[Visa/MC]'

If @showstore = 1
    begin
        set @result0 = 'StoreID,' + @result0
    end
If @showcashier = 1
    begin
        set @result0 = 'Cashier,' + @result0
    end
If @showregister = 1
    begin
        set @result0 = 'Register,' + @result0
    end
If @showdate = 1
    begin
        set @result0 = 'Date,' + @result0
    end
execute(@result)

1 个答案:

答案 0 :(得分:2)

您应该在所有@result之后的最后一行设置IF..ELSE的值:

DECLARE @result nvarchar(max),@result0 nvarchar(max)
SET @showstore = 1
SET @showcashier = 0
SET @showregister = 0
SET @showdate = 0  

set @result0 = 'Amex,[Gift Card],Debit,[Off Line C.Card],[Str Cr],[House Acct],Cash,Rebate,[C.Card],VIP,Discover,[Check],MasterCard,[Visa/MC]'

If @showstore = 1
    begin
        set @result0 = 'StoreID,' + @result0
    end
If @showcashier = 1
    begin
        set @result0 = 'Cashier,' + @result0
    end
If @showregister = 1
    begin
        set @result0 = 'Register,' + @result0
    end
If @showdate = 1
    begin
        set @result0 = 'Date,' + @result0
    end
set @result = 'select '+@result0+' from ##tempz'
execute(@result)