The following simple SQL example is returning an error.
Here's a table type that's passed to a table-valued function:
CREATE TYPE Ids
AS TABLE
(
Id int NOT NULL,
PRIMARY KEY( Id )
);
GO
And here is the table-valued function that fails:
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
The error returned is:
Msg 137, Level 16, State 1, Procedure GetIds, Line 28
Must declare the scalar variable "@ids".
I've read posts that say that it happens when the SQL compatibility level is too old, but the following returns 100:
SELECT compatibility_level
FROM sys.databases
WHERE name = 'TheDatabaseName';
Any suggestions would be greatly appreciated.
declare @ids table(Ids int)
– S.Visser