title: SQL Error: "Must declare the scalar variable" when passing a table parameter to a table-valued function
body: <p>The following simple SQL example is returning an error.</p>
  
  <p>Here's a table type that's passed to a table-valued function:</p>
  
  <pre><code>CREATE TYPE Ids
      AS TABLE
      (
          Id int NOT NULL,
          PRIMARY KEY( Id )
      );
  GO
  </code></pre>
  
  <p>And here is the table-valued function that fails:</p>
  
  <pre><code>CREATE FUNCTION GetIds
  (
      @ids -- or null
          Ids READONLY
  )
  RETURNS
      @result
          TABLE
          (
              EachId int
          )
  AS
  BEGIN
      IF @ids IS NOT NULL
          INSERT INTO @result
              SELECT Id
              FROM @ids;
      RETURN;
  END;
  GO
  </code></pre>
  
  <p>The error returned is:</p>
  
  <blockquote>
    <p>Msg 137, Level 16, State 1, Procedure GetIds, Line 28<br>
    Must declare the scalar variable "@ids".</p>
  </blockquote>
  
  <p>I've read posts that say that it happens when the SQL compatibility level is too old, but the following returns 100:</p>
  
  <pre><code>SELECT compatibility_level 
  FROM sys.databases 
  WHERE name = 'TheDatabaseName';
  </code></pre>
  
  <p>Any suggestions would be greatly appreciated.</p>