select和case的这种写法

Dision LI 2011-07-17 05:18:04

Declare @PartID as int
set @PartID =3
select 业务订单ID,
(case @PartID when 2 THEN 领用金额1,做数金额1
when 3 THEN 领用金额2,做数金额2 end)
from table


select ... from table 这里怎么写

请不要用if一个一个判断的方式,可否
...全文
122 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-07-17
  • 打赏
  • 举报
回复
Declare @PartID int
set @PartID =3
select
业务订单ID,领用金额=case @partid when 2 then 领用金额1 when 3 then 领用金额2 end,
做数金额=case @partid when 2 then 做数金额1 when 3 then 做数金额2 end
from
[table]
chuanzhang5687 2011-07-17
  • 打赏
  • 举报
回复
回答问题还要猜楼主的心思,叶子哥辛苦了
pt1314917 2011-07-17
  • 打赏
  • 举报
回复
哦手误,写错了。后面的是3.。写成2个2了
Declare @PartID int
set @PartID =3
select 业务订单ID,领用金额=case @partid when 2 then 领用金额1 when 3 then 领用金额2 end,
做数金额=case @partid when 2 then 做数金额1 when 3 then 做数金额2 end
from [table]

pt1314917 2011-07-17
  • 打赏
  • 举报
回复
Declare @PartID as int
set @PartID =3
select 业务订单ID,
(case @PartID when 2 THEN 领用金额1,做数金额1
when 3 THEN 领用金额2,做数金额2 end)
from table


Declare @PartID int
set @PartID =3
select 业务订单ID,领用金额=case @partid when 2 then 领用金额1 when 2 then 领用金额2 end,
做数金额=case @partid when 2 then 做数金额1 when 2 then 做数金额2 end
from [table]

叶子 2011-07-17
  • 打赏
  • 举报
回复
貌似明白了,楼主不想用case when ,不想用if。


Declare @PartID int;set @PartID =3

declare @sql varchar(8000)
set @sql='select 业务订单ID,领用金额'+ltrim(@PartID-1)+
',做数金额'+ltrim(@PartID-1)+' from table'
--select @sql
/*
select 业务订单ID,领用金额2,做数金额2 from table
*/
exec(@sql)
cd731107 2011-07-17
  • 打赏
  • 举报
回复
Declare @PartID as int
set @PartID =3
select 业务订单ID,
领用金额=(case @PartID when 2 THEN 领用金额1 when 3 THEN 领用金额2 end),
做数金额=(case @PartID when 2 THEN 做数金额1 when 3 THEN 做数金额2 end)
from tb
叶子 2011-07-17
  • 打赏
  • 举报
回复
没明白楼主要什么结果
但是case when then 后面两个字段中间用逗号隔开的写法是有问题的。

34,575

社区成员

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

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