SQL里面如果不满足条件就阻止执行接下来的SQL语句怎么做呢,我不要if判断只想知道有没有其他方法呢?

如题所述

--存储过程中使用事务(ATM机转账)
--事务 begin trans(开始事务) commit trans(提交事务) rollback trans(撤销)
--转账的操作 a,b 一个是增加,一个是减少, 当其中某个操作失败了,真个转账就失败
if object_id('pro_atm') is not null
drop proc pro_atm
go
create proc pro_atm
@a varchar(4)='',
@b varchar(4)='',
@money int
as--代码块
declare @sumerror int
set @sumerror=0
begin tran --开始事务
--对输入参数进行判断
if(@a='' or @b='' or @money<=0)
begin
raiserror('转账的账号或者金额不对,请重新输入!',16,1)
set @sumerror=@sumerror+@@error
return
end
print '转账之前的信息'
select * from account
--转账
update account set moneys=moneys+@money where id=@b
set @sumerror=@sumerror+@@error
update account set moneys=moneys-@money where id=@a
set @sumerror=@sumerror+@@error
print '转账之中的信息'
select * from account
--判断操作是否都成功
if(@sumerror=0)
begin
print '转账成功'
commit tran --提交事务 永久的改变表中的数据
end
else
begin
print '转账失败'
rollback tran --撤销之前的改变
end
print '转账之后的信息'
select * from account
--调用追问

事务我起初就想到了,只是因为是调用SQL语句就没写存储过程了,问题想了其他方法解决了,谢谢!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-25
1.exists 嵌套子查询如:
select * from tb_goods where exists(select * from tb_order where goodsid=tb_goods.id)
2.case when 多条件表达式:
case sex
when '1' then '男'
when '2' then '女'
else '其他' END
3.while条件,这个比较复杂,要增加变量去判断。本回答被网友采纳