woo*_*gie 13 sql t-sql sql-server
create function Xtest
(@d1 varchar(3)=null
,@d2 varchar(3)=null)
returns table
as
return
with code_list(code_pattern)
as
(
select x.code_pattern
from (values (@d1),(@d2)) as x(code_pattern)
where x.code_pattern is not null
),y
as
(
select substring(code,1,3) as code
from tblicd
where substring(code,1,3) in
(
select * from code_list
)
)
select * from y
Run Code Online (Sandbox Code Playgroud)
是我的功能,当它完全完成时才有意义.如果我尝试运行此查询并且我不提供两个参数,则会失败.我有一个与存储过程相同的代码,如果我只输入一个参数,它工作正常,并指定第二个参数null.null如果没有提供参数,有没有办法可以作为参数值传递,就像存储过程可以做的那样?
该函数确实编译.
Aar*_*and 40
调用函数时,不能省略参数.这不是你在语法上做错的事情,它只是不被SQL Server支持.存储过程具有可选参数,但功能没有.
但是,您可以提供default:
SELECT code FROM dbo.Xtest(default, default);
Run Code Online (Sandbox Code Playgroud)
或者在这种情况下,如果你知道默认值,NULL你可以这样做:
SELECT code FROM dbo.Xtest(NULL, NULL);
Run Code Online (Sandbox Code Playgroud)
另外,在创建和引用任何对象(尤其是函数)时,请始终使用模式前缀(在本例中dbo.).