1

I have a simple custom type for use it as table valued parameter. Like this:

 CREATE TYPE dbo.PeriodsList AS TABLE
    (
        PeriodDate NVARCHAR(8) NOT NULL
    )

I also have very simple table valued function:

alter FUNCTION GetPeriodsInYear (@periods  dbo.PeriodsList READONLY) returns @PeriodsSet Table(period NVARCHAR(8))

BEGIN
insert @PeriodsSet
select 
    '0' as period 
Return
end

But when I try to execute this function in this way

DECLARE @periods1 dbo.PeriodsList
INSERT INTO @periods1
VALUES ('20130916')

select * from GetPurchasesInYear(@periods1)

I receive error message - "Must declare the scalar variable "@periods1". I have found many examples about stored procedures but not about functions. Is it possible to pass table valued parameters into functions? And where the mistake in my code?

4

1 回答 1

1

检查 SQL Server 2008 数据库的“兼容性级别”。如果它设置为 80 (SQL Server 2000),当您尝试调用采用表值参数的函数(但不是存储过程)时,您将收到您所描述的错误。

如果兼容级别值为 90 (SQL Server 2005) 或更高,它应该可以工作。

要查看数据库的兼容性级别,请执行以下操作:

SELECT compatibility_level
FROM sys.databases 
WHERE name = 'YOUR_DB_NAME_HERE';

来源: http: //msgroups.net/microsoft.public.sqlserver.programming/pass-table-valued-para/97879

更改兼容级别 (SQL Server 2008):http ://technet.microsoft.com/en-us/library/bb510680%28v=sql.100%29.aspx

于 2014-04-02T21:14:37.280 回答