SQL用户定义的函数,表变量作为输入&返回值

时间:2017-01-12 12:26:32

标签: sql-server tsql user-defined-functions table-variable

我有一个用户定义的表值函数,其输入类型为表变量,返回类型是另一个表变量。

但是,当我尝试执行该函数时,它会抛出错误“必须声明标量变量”。看起来对函数的调用不知道表变量@myTable,即使它是在前一行声明的。

我觉得我疯了......你能发现我做错了吗?

-- Create the table type
CREATE TYPE [dbo].[TableType] AS TABLE(
    [z] [varchar](50) NULL
)
GO


-- Create the user defined function
USE [databaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[boxPackingSort3](@TableName TableType READONLY)
RETURNS @ReturnTable TABLE(
    [partno] [varchar](50) NULL,
    [Q] [int] NULL,
    [L] [int] NULL,
    [W] [int] NULL,
    [H] [int] NULL,
    [Id] [int] NULL
)
AS
BEGIN
    INSERT INTO @ReturnTable SELECT 'xxx', '1' , '1', '1', '1', '1' 
    RETURN
END


-- Call the user defined function
DECLARE @myTable TableType
INSERT INTO @myTable(z) VALUES('aaa')
select * from  dbo.[boxPackingSort3](@myTable)



Msg 137, Level 16, State 1, Line 5
Must declare the scalar variable "@myTable".

0 个答案:

没有答案