sql中case的应用

sailormars 2008-01-09 04:08:31
declare @sex nvarchar(50)
select @sex = TitleOfCourtesy from dbo.Employees
print @sex
--case 的使用
declare @sex nvarchar(50)
select @sex = TitleOfCourtesy from dbo.Employees
where EmployeeID = 5
case @sex
when 'Ms.' then print'女士'
when 'Mr.' then print'男士'
else print'nothing'
end
请问这样用错误在哪里?
...全文
697 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sailormars 2008-01-10
  • 打赏
  • 举报
回复
select @sex = TitleOfCourtesy from dbo.Employees
where EmployeeID = 5
print @sex
print case @sex
when 'Ms.' then '女士'
when 'Mr.' then '男士'
else 'nothing'
end
我已经明白了,case只能用在表达式中.
pt1314917 2008-01-10
  • 打赏
  • 举报
回复

如果要简洁,二楼的已经够了。
楼主是要将你的改成这样?
declare @sex nvarchar(50)
select @sex = TitleOfCourtesy from dbo.Employees where EmployeeID = 5
select case @sex
when 'Ms.' then '女士'
when 'Mr.' then '男士'
else 'nothing' end

还是:
declare @sex nvarchar(50)
select @sex = TitleOfCourtesy from dbo.Employees where EmployeeID = 5
if(@sex='Ms.') print '女士'
if(@sex='Mr.') print '男士'
else print 'nothing'

sailormars 2008-01-10
  • 打赏
  • 举报
回复
1楼的根本不对,执行报错
2楼的,我就想用我那种用法,可以用吗?
dawugui 2008-01-09
  • 打赏
  • 举报
回复
select TitleOfCourtesy = case TitleOfCourtesy when 'Ms.' then 女士' when 'Mr.' then '男士'  end from dbo.Employees 
kk19840210 2008-01-09
  • 打赏
  • 举报
回复
declare   @sex   nvarchar(50) 
select top 1 @sex = TitleOfCourtesy from dbo.Employees
print @sex
--case 的使用
declare @sex nvarchar(50)
select top 1 @sex = TitleOfCourtesy from dbo.Employees
where EmployeeID = 5

select case @sex
when 'Ms.' then print'女士'
when 'Mr.' then print'男士'
else print'nothing'
end

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧