我真的可以使用一些帮助来编写使用以下逻辑的多语句表值函数:
- 接受 varchar 参数作为输入
- 如果参数以 A 开头,则运行 select 语句
- 如果没有返回行,则运行 select 语句
- 如果参数不以 A 开头,则运行 select 语句
- 如果没有返回行,则运行 select 语句
- 返回一个输出表,这将是上面的结果集之一,具体取决于参数
到目前为止,这就是我所拥有的:
CREATE FUNCTION dbo.CheckAccess
(@UserName varchar(30))
RETURNS @AccessTable TABLE (User varchar(max), Access varchar(max))
AS
BEGIN
IF LEFT(@UserName,1) = 'A'
INSERT INTO @AccessTable
SELECT
CASE
WHEN EXISTS (SELECT User, Access FROM dbo.AccessTable1 WHERE User = @UserName)
THEN (SELECT User, Access FROM dbo.AccessTable1 WHERE User = @UserName)
ELSE (SELECT @UserName User, 'No Access' Access)
END
ELSE
INSERT INTO @AccessTable
SELECT
CASE
WHEN EXISTS (SELECT User, Access FROM dbo.AccessTable2 WHERE User = @UserName)
THEN (SELECT User, Access FROM dbo.AccessTable2 WHERE User = @UserName)
ELSE (SELECT @UserName User, 'No Access' Access)
END
RETURN
END;
不确定我在这里缺少什么,但我收到以下错误:
Msg 116, Level 16, State 1, Procedure CheckAccess, Line 90
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 116, Level 16, State 1, Procedure CheckAccess, Line 92
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 213, Level 16, State 1, Procedure CheckAccess, Line 11
Column name or number of supplied values does not match table definition.
我知道如何在存储过程中使用临时表相当容易地做到这一点,但是由于它们不是函数中的选项,所以我遇到了一些困难。如果有人可以提供一些建议,我将不胜感激。提前致谢。