SQL中的if...else如何嵌套

tonyze 2009-02-16 04:20:08
我跟踪了一下,生成如下代码(有错误)

if((select count(*) from eap_history)=10)

update eap_history set Name='收款单',url='ARAP/ReceiveVoucherEdit.aspx?SourceType=FromMenu',code='ARAP01',recordtime='2009-02-16 15:58:04' where recordtime =(select min(recordtime) from eap_history)

else

if(exists(select * from eap_history where UserID ='8bbccdc8-b137-4a33-b253-7929b4473157' and code ='ARAP01'))
update eap_history set Name='收款单',url='ARAP/ReceiveVoucherEdit.aspx?SourceType=FromMenu',code='ARAP01',recordtime='2009-02-16 15:58:04' where userid='8bbccdc8-b137-4a33-b253-7929b4473157' and code ='ARAP01'

else

insert into eap_history values( 'a5a297b3-095f-4011-b210-c3d3af11b535', '收款单','ARAP/ReceiveVoucherEdit.aspx?SourceType=FromMenu','ARAP01','8bbccdc8-b137-4a33-b253-7929b4473157','2009-02-16 15:58:04')
end
...全文
4587 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
小坏 2010-09-11
  • 打赏
  • 举报
回复
学习了
wjcper2008 2009-08-06
  • 打赏
  • 举报
回复
aaaa
weijianliang 2009-08-02
  • 打赏
  • 举报
回复
学习. 谢谢.
QQ306832666 2009-02-16
  • 打赏
  • 举报
回复
顶 楼上
xray2005 2009-02-16
  • 打赏
  • 举报
回复 1

if((select count(*) from eap_history)=10)
begin
update eap_history set Name='收款单',url='ARAP/ReceiveVoucherEdit.aspx?SourceType=FromMenu',code='ARAP01',recordtime='2009-02-16 15:58:04' where recordtime =(select min(recordtime) from eap_history)
end
else
begin
if(exists(select * from eap_history where UserID ='8bbccdc8-b137-4a33-b253-7929b4473157' and code ='ARAP01'))
begin
update eap_history set Name='收款单',url='ARAP/ReceiveVoucherEdit.aspx?SourceType=FromMenu',code='ARAP01',recordtime='2009-02-16 15:58:04' where userid='8bbccdc8-b137-4a33-b253-7929b4473157' and code ='ARAP01'
end
else
begin
insert into eap_history values( 'a5a297b3-095f-4011-b210-c3d3af11b535', '收款单','ARAP/ReceiveVoucherEdit.aspx?SourceType=FromMenu','ARAP01','8bbccdc8-b137-4a33-b253-7929b4473157','2009-02-16 15:58:04')
end
end


stone_1207 2009-02-16
  • 打赏
  • 举报
回复
基本上结构都一样,只是sql中{}用Begin和end代替
我姓区不姓区 2009-02-16
  • 打赏
  • 举报
回复
加上begin和end,就相当于c#中的{和}
the_pain 2009-02-16
  • 打赏
  • 举报
回复
sql中的if else

如果说条件下面只有一行的话就不用加begin end,不加的话则只执行if或else下的第一个SQL

若有多行的话
if
begin
.....
....
end
else
begin
...
..
...
end
真相重于对错 2009-02-16
  • 打赏
  • 举报
回复
IF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') < $15
BEGIN
PRINT 'The following titles are excellent mod_cook books:'
PRINT ' '
SELECT SUBSTRING(title, 1, 35) AS Title
FROM titles
WHERE type = 'mod_cook'
END
ELSE
PRINT 'Average title price is more than $15.'

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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