无法将结果存储到变量SQL中

时间:2014-05-08 20:14:39

标签: sql sql-server stored-procedures

今天就有一个普遍的问题。我试图将结果存储在变量中,但它不起作用。我不是想做任何花哨而是简单的任务。见下文:

    declare @prizeid bigint;
    declare @today datetime;
    declare @dayOfMonth int;
    declare @year int;
    declare @month int;
    select @today = getdate();
    select @dayOfMonth = Day(@today);
    select @year = Year(@today);
    select @month = Month(@today);

    if @month = 1
    begin
        select @month = 12
        select @year = @year - 1
    end
    else select @month = @month - 1; 




declare @sqlQuery varchar(250);
declare @quantityForSnapShot bigint;
declare @filename varchar(25);

set @prizeid=31
set @filename = 'Prizes_' + REPLACE(STR(@month, 2, 0), ' ', '0') + '_' + ltrim(str(@year));

select @sqlQuery = 'select Quantity from ' + @filename +
' where PrizeID=' + convert(varchar,@prizeid)
EXEC @quantityForSnapShot=@sqlQuery
print @quantityForSnapShot

我真正想要的是检索数量并将其存储在var @quantityForSnapShot中。

: - )

2 个答案:

答案 0 :(得分:2)

declare @prizeid bigint;
declare @today datetime;
declare @dayOfMonth int;
declare @year int;
declare @month int;
select @today = getdate();
select @dayOfMonth = Day(@today);
select @year = Year(@today);
select @month = Month(@today);

if (@month = 1)
    begin
        select @month = 12
        select @year = @year - 1
    end
else 
   begin
      select @month = @month - 1; 
   end

declare @sqlQuery nvarchar(MAX);  --<-- to be on safe side
declare @quantityForSnapShot bigint;
declare @filename varchar(25);

set @prizeid=31
set @filename = 'Prizes_' + REPLACE(STR(@month, 2, 0), ' ', '0') + '_' + ltrim(str(@year));

select @sqlQuery = N' select @quantityForSnapShot = Quantity ' +
                   N' from ' + QUOTENAME(@filename) +
                   N' where PrizeID = @prizeid'

EXECUTE sp_executesql @sqlQuery   
                     ,N'@prizeid bigint, @quantityForSnapShot bigint OUTPUT'  
                     ,@prizeid , @quantityForSnapShot OUTPUT

SELECT @quantityForSnapShot

您正在尝试调用此Dynamic sql,因为它是具有返回值的存储过程。您需要使用OUTPUT参数从动态sql中检索@quantityForSnapShot变量的值。

我还使用QUOTENAME函数在表名周围加上方括号[],以明确告诉sql server它是一个对象名。进入的一个好习惯,因为它可以保护您免受Sql注入攻击。

还使用系统存储过程sp_executesql来执行动态sql。

答案 1 :(得分:0)

试试这个..

    Begin Tran

    declare @prizeid bigint;
    declare @today datetime;
    select @today = getdate();
    declare @dayOfMonth int;
    select @dayOfMonth = Day(@today);
    declare @year int;
    select @year = Year(@today);
    declare @month int;
    select @month = Month(@today);


    if @month = 1
    begin
    select @month = 12
    select @year = @year - 1
    end
    else select @month = @month - 1; 


    declare @sqlQuery varchar(250);
    declare @quantityForSnapShot bigint;
    declare @filename varchar(25);

    set @prizeid=31
    set @filename = 'Prizes_' + REPLACE(STR(@month, 2, 0), ' ', '0') + '_' + ltrim(str(@year));

    select @sqlQuery = 'select Quantity from ' + @filename +
    ' where PrizeID=' + convert(varchar,@prizeid)
    Set @quantityForSnapShot = @sqlQuery

    Create Table #tmp ( Quantity bigint)

    INSERT INTO  #tmp (Quantity)
    EXEC (@sqlQuery)

    Set @quantityForSnapShot = (Select Quantity From #tmp)
    Select @quantityForSnapShot
          print @quantityForSnapShot

    Rollback Tran