写存储过程 查询表,得到俩个整数做比较,如果a>b,status=0,否则status=1

mmkkuoi 2012-05-13 10:23:11
写存储过程 查询表,得到俩个整数做比较,如果a>b,status=0,否则status=1
create table test
(
a int ,
b.int ,
statur int default 0
)

insert into test values(1,12,0),(32,2,0),(1,12,0),(1,12,0),(1,12,0),(1,12,0);
...全文
122 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复


create table test
(
a int ,
b int ,
statur int default (0)
)

insert into test values(1,12,0),(32,2,0),(1,12,0),(1,12,0),(1,12,0),(1,12,0);

update test
set statur=(case when a>b then 0 else 1 end),
a=(case when a>b then b else a end)

select * from test

/*
a b statur
1 12 1
2 2 0
1 12 1
1 12 1
1 12 1
1 12 1
*/
  • 打赏
  • 举报
回复

update test
set statur=(case when a>b then 0 else 1 end,
a=(case when a>b then b else a end)

这样不可以吗?
mmkkuoi 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code


update test
set statur=(case when a>b then 0 else 1 end)

--直接更新就好了吧,不用存储过程哈
[/Quote]
如果我要根据当前的条件再update set a=b

update test
set statur=(case when a>b then 0 else 1 end)
a=(case when a>b then b else a end)
有没有办法不用在重复一次case when a>b



anzhiqiang_touzi 2012-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
SQL code

create table test
(
a int ,
b int ,
STATUS AS (CASE WHEN a > b THEN 0 ELSE 1 END) ) --用计算列来的更直接些


INSERT INTO test(a,b)
SELECT 1,2
UNION ALL
SELECT 3,4
UNION ALL
SELECT 6,5
……
[/Quote]
Mr_Nice 2012-05-14
  • 打赏
  • 举报
回复
create table test
(
a int ,
b int ,
STATUS AS (CASE WHEN a > b THEN 0 ELSE 1 END) ) --用计算列来的更直接些


INSERT INTO test(a,b)
SELECT 1,2
UNION ALL
SELECT 3,4
UNION ALL
SELECT 6,5

SELECT * FROM Test


/*
a b STATUS
1 2 1
3 4 1
6 5 0*/

  • 打赏
  • 举报
回复

update test
set statur=(case when a>b then 0 else 1 end)

--直接更新就好了吧,不用存储过程哈

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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