title: "Must declare the table variable "@name"" in stored procedure
body: <p>I have a procedure which returns the error:</p>
  
  <blockquote>
    <p>Must declare the table variable "@PropIDs".</p>
  </blockquote>
  
  <p>But it is followed with the message:</p>
  
  <blockquote>
    <p>(123 row(s) affected)</p>
  </blockquote>
  
  <p>The error appears when I execute it with</p>
  
  <pre><code>EXEC [dbo].[GetNeededProperties] '1,3,5,7,2,12', '06/28/2013', 'TT'
  </code></pre>
  
  <p>But works fine when</p>
  
  <pre><code>EXEC [dbo].[GetNeededProperties] NULL, '06/28/2013', 'TT'
  </code></pre>
  
  <p>Can any one help me with that?
  The procedure:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[GetNeededProperties]
  @NotNeededWPRNs nvarchar(max), --string like '1,2,3,4,5'
  @LastSynch datetime,
  @TechCode varchar(5)
  AS
  BEGIN
  
  DECLARE @PropIDs TABLE
  (ID bigint)
  Declare @ProductsSQL nvarchar(max);
  SET @ProductsSQL = 'Insert into @PropIDs (ID) 
  SELECT [WPRN] FROM [dbo].[Properties] WHERE(WPRN in (' + @NotNeededWPRNs + '))'
  exec sp_executesql @ProductsSQL
  
  SELECT  p.WPRN AS ID,
  p.Address  AS Address,
  p.Address AS Street
    FROM [dbo].[Properties] AS p
  WHERE 
     p.WPRN NOT IN( SELECT ID FROM @PropIDs)
  </code></pre>
  
  <hr>
  
  <p>I've found kind of solution when declaring table like this:</p>
  
  <pre><code>IF OBJECT_ID('#PropIDs', 'U') IS NOT NULL
    DROP TABLE #PropIDs
  
  CREATE  TABLE  #PropIDs
  </code></pre>
  
  <p>But when execute the procedure from C# (linq sql) it returns an error</p>