请参见下面的示例,
CREATE TABLE Test
(
CD varchar(10) not null
)
GO
INSERT INTO Test VALUES ('TEST')
GO
CREATE TYPE [CdTable] AS TABLE (CD varchar(10));
GO
CREATE FUNCTION TestTbl ( @x varchar(10), @c CdTable READONLY )
RETURNS TABLE
AS
RETURN
SELECT t.CD
FROM test t
JOIN @c c ON t.CD = c.CD OR c.CD IS NULL
WHERE t.CD = @x
GO
DECLARE @tt AS CdTable;
INSERT INTO @tt VALUES ('TEST');
SELECT * FROM TestTbl('TEST', @tt);
DELETE FROM @tt;
INSERT INTO @tt VALUES (NULL);
SELECT * FROM TestTbl('TEST', @tt);
GO
CREATE FUNCTION TestNoTbl ( @x varchar(10) )
RETURNS TABLE
AS
RETURN
SELECT *
FROM TestTbl(@x, ???? ) --<<<< how do I pass inline @c parameter with only a single row set to NULL
GO
我想找出一种方法可以:
我找不到任何办法。
有人可以帮忙吗?
ps:我使用的是Microsoft SQL Server 2012(或更高版本)
最佳答案
首先,您无法为Table Valued parameter
分配默认值。相反,您可以更改功能。
将INNER JOIN
更改为LEFT OUTER JOIN
SELECT t.CD
FROM test t
LEFT OUTER JOIN @c c ON t.CD = c.CD
WHERE t.CD = @x
即使
@c
不保存任何记录,您也将基于@x过滤器获得结果更新:根据您的评论
If exists(select 1 from @c)
SELECT t.CD
FROM test t
JOIN @c c ON t.CD = c.CD
WHERE t.CD = @x
Else
SELECT t.CD
FROM test t
WHERE t.CD = @x
关于sql - 表值参数默认值或通过内联,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37293439/