sql trigger

nonamedemo發表於2007-08-14


create database mydb
go
use mydb
go
drop table stuinfo
drop table stumarks

if exists(select * from sysobjects where name ='bank')
drop table bank
go
create table bank
(
cardid varchar(20) not null primary key,
customname varchar(10) not null ,
currentmoney money not null check(currentmoney>=1)

)
if exists (select * from sysobjects where name='transinfo')
drop table transinfo
go
create table transinfo
(
cardid varchar(20) not null foreign key references bank(cardid),
chargetype varchar(4) not null ,
tmoney money
)
insert into bank values('10001000','張三',120000)
insert into bank values('10001001','李四',1)
create proc look
as
select * from bank
select * from transinfo
go

set nocount on

exec look


--insert觸發器
create trigger transinfo_trig_inserte on transinfo with encryption
for insert
as
begin transaction

declare @money money,
@cardid varchar(20),
@type varchar(4),
@bankmoney money
select @type=chargetype,@money=tmoney,@cardid=cardid from inserted
select @bankmoney=currentmoney from bank where
if(@type='支取')
begin
if(@bankmoney-@money>1)
begin
update bank set currentmoney=currentmoney - @money where
commit transaction
end
else
begin
print'操作失敗,餘額不足!'
rollback transaction
end
end
else
begin
update bank set currentmoney=currentmoney + @money where
commit transaction
end
exec look
go

insert into transinfo values('10001001','存入',1000)
insert into transinfo values('10001001','支取',20000)
insert into transinfo values('10001001','支取',200)
insert into transinfo values('10001000','支取',1000)
--delete觸發器
alter trigger transinfo_trig_del on transinfo with encryption
for delete
as

declare @cardid varchar(20),
@money money,
@cmoney money,
@type varchar(4)
select @cardid =cardid,@money=tmoney,@type=chargetype from deleted
insert into newtable values(@cardid,@type,@money)
go
delete from transinfo where cardid ='10001001'
exec look
select * from newtable
--update觸發器
exec look
alter trigger transinfo_trig_update on transinfo with encryption
for update
as
begin transaction
declare @cardid varchar(20),
@oldmoney money,
@newmoney money,
@type varchar(4)
select @cardid =cardid ,@newmoney=tmoney,@type=chargetype from inserted
select @oldmoney=tmoney from deleted
select @oldmoney , @newmoney
--if(@newmoney>20000)
--rollback transaction
if(@type='支取')
begin
if((@oldmoney - @newmoney)>20000)
begin
print'款額太高,更新不能完成!'
rollback transaction
end
else
begin
update bank set currentmoney = currentmoney - @newmoney where
commit transaction
end
end
else if(@type='存入')
begin
update bank set currentmoney = currentmoney + @newmoney where
end
else
begin
print'本次更新操作不能正常執行'
rollback transaction
end

--測試
update transinfo set cardid='10001000',chargetype='支取',tmoney=20001 where cardid='10001000'
exec look

update bank set currentmoney =100000 where cardid='10001000'


create trigger bank_trig_update on bank for update
as
declare @oldmoney money,
@newmoney money,
@cardid varchar(20)
select @oldmoney = currentmoney from deleted
select @newmoney = currentmoney from inserted
if((@oldmoney - @newmoney)>20000)
rollback transaction

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-957239/,如需轉載,請註明出處,否則將追究法律責任。

相關文章