“Must declare the table variable ”@name“” in stored procedure

前端 未结 4 2139
忘了有多久
忘了有多久 2021-02-19 05:56

I have a procedure which returns the error:

Must declare the table variable \"@PropIDs\".

But it is followed with the message:

4条回答
  •  庸人自扰
    2021-02-19 06:13

    Change you code to :

    Declare @ProductsSQL nvarchar(max);
    SET @ProductsSQL = 'DECLARE @PropIDs TABLE
    (ID bigint);
    Insert into @PropIDs (ID) 
    SELECT [WPRN] FROM [dbo].[Properties] WHERE(WPRN in (' + @NotNeededWPRNs + '))'
    exec sp_executesql @ProductsSQL
    

    Table variable declared outside the dynamic SQL will not be available to the dynamic SQL.

提交回复
热议问题