sql trigger
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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-957239/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Database Logoff Trigger SQLDatabaseGoSQL
- TRIGGER裡的動態SQLSQL
- PL/SQL 07 觸發器 triggerSQL觸發器
- SQL指令碼建立trigger,owner用變數出錯。SQL指令碼變數
- Mysql TriggerMySql
- jQuery trigger()jQuery
- ±±oracle trigger±±Oracle
- 使用after create 建立trigger記錄procedure PL/SQL程式碼變更SQL
- Test Oracle triggerOracle
- Oracle trigger問題Oracle
- SqlServer 2005 TriggerSQLServer
- python trigger 組合Python
- COMPOUND TRIGGER學習
- logon_triggerGo
- oracle trigger 同步資料Oracle
- Create a trigger TO monitoring DDL
- WPF TryFindResource typeof Setter Trigger
- trigger 'SYS.GGS_DDL_TRIGGER_BEFORE' is invalid and failed re-validationAI
- Mysql之觸發器triggerMySql觸發器
- /proc/sysrq-trigger檔案
- PLSQL學習-【8trigger】SQL
- 禁止DDL執行的trigger
- MySQL入門-- TRIGGER(觸發器)MySql觸發器
- triggerHandler()和trigger()區別
- postman trigger xdebug session in phpstormPostmanSessionPHPORM
- 融會貫通學習trigger
- 限制資料庫登陸trigger資料庫
- trigger和物化檢視同步表
- Query to Check SP/Table/Trigger Exists in Database or notDatabase
- oracle trigger語法小記(一)Oracle
- SQLite建立觸發器 CREATE TRIGGERSQLite觸發器
- oracle trigger觸發器這servererrorOracle觸發器ServerError
- [hudson]hudson的build trigger解釋UI
- 透過TRIGGER限制IP登入
- Oracle Instead of Trigger的用法Oracle
- Oracle logon trigger舉例OracleGo
- MySQL 事件監聽 huangdijia/Laravel-triggerMySql事件Laravel
- 禁止oracle表的觸發器triggerOracle觸發器