CREATE FUNCTION [dbo].[fn_actions]
(
@roleid varchar(36)
)
RETURNS TABLE
AS
RETURN
select *
from actions
where action_type_id in (
select action_type_id
from action_roles
where role_id = isnull(@roleid,role_id)
)
上面的函数从与提供的@roleid 参数匹配的actions_roles 表中返回一个操作列表。但是,actions 表中的某些操作在 action_roles 表中根本不存在。因此,当指定 NULL 参数时,我希望函数简单地返回
select * from actions
我尝试使用 if 语句,但这似乎不适用于内联表函数。
CREATE FUNCTION [dbo].[fn_actions]
(
@roleid varchar(36)
)
RETURNS TABLE
AS
RETURN
IF (@roleid is NULL)
BEGIN
select * from actions
END
ELSE
select *
from actions
where action_type_id in (
select action_type_id
from action_roles
where role_id = isnull(@roleid,role_id)
)
实现这一目标的最佳方法是什么?