SQL Server 05 - 表值函数 - 执行不同选择基于传递参数的值?
问题描述:
我想实现一个解决方案,其中Table-Valued-Function基于传递给它的参数的值返回不同的数据集。SQL Server 05 - 表值函数 - 执行不同选择基于传递参数的值?
例如:这样的事情:
CREATE FUNCTION test(@param char(6))
RETURNS TABLE
AS
RETURN
(
IF @param IS NULL
select * from this
ELSE
select * from that
END
)
答
您可以重新编写函数为:
CREATE FUNCTION test(@param char(6))
RETURNS @retInformation TABLE
(
-- Columns returned by the function
ID int PRIMARY KEY NOT NULL
)
AS
-- Returns the required Id if input parameter is null.
BEGIN
IF @param IS NULL
BEGIN
INSERT @retInformation
select ID from this
END
ELSE
BEGIN
INSERT @retInformation
select ID from that
END;
RETURN;
END;
什么是你所面临的问题? – Deepshikha 2014-10-08 05:49:05
@Deepshikha它给了我错误:关键字'IF'附近的语法无效,关键字''附近的语法无效' – Ocelot 2014-10-08 05:51:35