sql server存储过程中怎样写if

sql server存储过程中怎样写if,第1张

1
2
3
4
5
6
7
8
9
10
11
12

if a>b
Begin
print 'a'
End
Else if a<b
Begin
print 'b'
End
Else
Begin
print '代码'
End
if-else语法为,最好不要把begin end省略掉。否则,存储过程比较长可能会报语法错误

sql中没有if语句,但有case语句,而且是所有数据库都支持的,
语法是case
when
条件1
then
结果1
when
条件2
then
结果2
else
结果n
end,
可以有任意多个条件,如果没有默认的结果,最后的else也可以不写

可以实现,以sql server为例看:
if not exists(select userName from food join diningcar on foodfoodId=diningcarfoodId join users on diningcaruserId=usersuserId where (comment=0 or comment=-1) and userName='zq' group by userName)
select 0,'zq'
else
select sum(price),userName from food join diningcar on foodfoodId=diningcarfoodId join users on diningcaruserId=usersuserId where (comment=0 or comment=-1) and userName='zq' group by userName
方法二:
select isnull(sum(price),0),userName
from food join diningcar on foodfoodId=diningcarfoodId
join users on diningcaruserId=usersuserId
where (comment=0 or comment=-1) and userName='zq'
group by userName
不知道是不是你想要的结果,但是我有个疑问,你为什么不在程序里进行判断,而是要让sql语句判断呢?

不知道您指的是什么的sql脚本。
如果是数据库语言的话大概是这么写:
update “表名”
set ID=“0”
where ID>“30”
不过不同数据库的语言可能有细微变化。。。
希望对您有所帮助,谢谢。

select 姓名,
case when 成绩 < 60 then '不及格'
when 成绩 >=60 and 成绩 <70 then '及格'
when 成绩 >=70 and 成绩 <80 then '中等'

end as 等级 from 表


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13410718.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-30
下一篇 2023-07-30

发表评论

登录后才能评论

评论列表(0条)

保存