为什么 微软 sql 语句 用 begin end 恩
为什么 微软 sql 语句 用 begin end 恩
--修改触发器,触发条件为--insert语句。
--触发的列为 [dbo].[trig_inGoods] 表 [dbo].[tb_InStore]
ALTER TRIGGER [dbo].[trig_inGoods] ON [dbo].[tb_InStore]
FOR insert
-----判断条件1
if exists(select GoodsID from inserted
where GoodsID in(select GoodsID from tb_GoodsInfo))
-----满足条件1,执行下面的语句
begin
-----判断条件2
if exists(select StoreName from inserted
where StoreName in(select StoreName from tb_GoodsInfo))
-----满足条件2执行下面的语句
begin
update tb_GoodsInfo set GoodsNum=GoodsNum+(select GoodsNum
from inserted) where GoodsID=(select GoodsID from inserted)
and GoodsName=(select GoodsName from inserted) and StoreName=(
select StoreName from inserted) and GoodsSpec=(
select GoodsSpec from inserted)
end
-----满足条件1,不满足条件2
else
-----执行 满足条件1,不满足条件2 的语句
begin
insert into tb_GoodsInfo(GoodsID,GoodsName,StoreName,GoodsSpec,
GoodsUnit,GoodsNum,GoodsInPrice,GoodsOutPrice)
select GoodsID,GoodsName,StoreName,GoodsSpec,GoodsUnit,
GoodsNum,GoodsPrice,GoodsPrice*110/100 from inserted
end
end
------不满足条件1,执行的语句
else
begin
insert into tb_GoodsInfo(GoodsID,GoodsName,StoreName,GoodsSpec,
GoodsUnit,GoodsNum,GoodsInPrice,GoodsOutPrice)
select GoodsID,GoodsName,StoreName,GoodsSpec,GoodsUnit,
GoodsNum,GoodsPrice,GoodsPrice*110/100 from inserted
end
------解决方案--------------------
程序代码的开始与结束.用begin和end最准确.我感觉.