0
我需要創建一個函數,它將一個表(或表變量)作爲輸入參數,並返回一個表值作爲參數。這可能有以下限制:是否可以創建一個將表作爲參數並返回表的函數?
- SQL Server 2005的
- CLR函數不是一個選項(應該是TSQL僅)
任何示例代碼作爲首發將是有益的。
我需要創建一個函數,它將一個表(或表變量)作爲輸入參數,並返回一個表值作爲參數。這可能有以下限制:是否可以創建一個將表作爲參數並返回表的函數?
任何示例代碼作爲首發將是有益的。
不能使用表參數類型,直到SQL Server 2008的
一種選擇是使用XML表格中和XPath通過解析它。
我試過了,但是沒有,@gbn是正確的,你不能在SQL Server 2005
這裏是我的嘗試:
CREATE FUNCTION dbo.TestTable
(
@InputTable table (RowID int, DataValue varchar(10))
)
RETURNS
table (RowID int, DataValue varchar(10))
AS
SELECT * FROM @InputTable ORDER BY 1 DESC
RETURN
go
DECLARE @t table (RowID int, DataValue varchar(10))
INSERT INTO @t VALUES (3,'cccc')
INSERT INTO @t VALUES (2,'bbbb')
INSERT INTO @t VALUES (1,'aaaa')
select * from @t
select * from dbo.TestTable(@t)
這裏的錯誤:
Msg 156, Level 15, State 1, Procedure TestTable, Line 3
Incorrect syntax near the keyword 'table'.
Msg 1087, Level 15, State 2, Procedure TestTable, Line 8
Must declare the table variable "@InputTable".
Msg 1087, Level 15, State 2, Line 1