标量函数中的 Case 语句用法
有人可以解释为什么这段代码对我不起作用吗?
create function dbo.fn_dis (@num int)
returns nvarchar(30)
as
begin
declare @catch nvarchar(30)
case
when @num = 1
then set @catch = 'Active'
else set @catch = 'Discontinued'
end
return @catch
end
另一方面,if 语句工作得很好:
create function dbo.fn_dis (@num int)
returns nvarchar(30)
as
begin
declare @catch nvarchar(30)
if @num = 1
set @catch = 'Active'
else
set @catch = 'Discontinued'
return @catch
end
我没有发现在网络上的标量函数中使用 case 语句有任何限制,
非常感谢您的帮助!赞赏:)
Can somebody explain why this code won't work for me?
create function dbo.fn_dis (@num int)
returns nvarchar(30)
as
begin
declare @catch nvarchar(30)
case
when @num = 1
then set @catch = 'Active'
else set @catch = 'Discontinued'
end
return @catch
end
On the other hand an if statement works just fine:
create function dbo.fn_dis (@num int)
returns nvarchar(30)
as
begin
declare @catch nvarchar(30)
if @num = 1
set @catch = 'Active'
else
set @catch = 'Discontinued'
return @catch
end
I didn't find any restriction on using case statements within scalar functions on the web
Thanks a lot for the help ! appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您确实需要一个函数,那么您至少可以将其简化为一条语句...但是,这似乎是一个简单的
内联案例会更好
只是为了扩展我的内联评论
If you really need a function for this, you can at least bring it down to a single statement... However, it seems like a simple
inline case would be better
Just to expand my inline comment