觸發器小例子!

2021-05-12 21:58:45 字數 1531 閱讀 8497

--insert 觸發器

create trigger tri_infodetails_i on info_details

after insert

asdeclare @id int

begin

--delete from info_details where id=

select @id=id from inserted;

insert into info_details_index(type,title,content,post_time,flag)

select type,title,content,getdate(),1 from info_details where id=@id;

--update info_details_index set content=content

end;

-- update觸發器

--select top 0 type,title,content,getdate() as post_time,1  as flag  into info_details_index from info_details;

create trigger tri_infodetails_u on info_details

after update

asdeclare @id int

begin

if exists(select 1 from inserted)

if exists(select 1 from deleted)

begin

select @id=id from inserted;

insert into info_details_index(type,title,content,post_time,flag)select type,title,content,getdate(),-1 from info_details where id=@id;

insert into info_details_index(type,title,content,post_time,flag)select type,title,content,getdate(),1 from info_details where id=@id;

end--update info_details_index set content=content

end--delete觸發器

create trigger tri_infodetails_d on info_details

after delete

asdeclare @id int

begin

if exists(select 1 from deleted)

begin

insert into info_details_index(type,title, post_time,flag)

select type,title, getdate(),-1 from deleted info_details ;

endend

SQL觸發器小例子

在orders表上建立乙個觸發器叫bak order create trigger bak order on orders 觸發條件為 在該orders表上做insert操作後才出發 也可以在加上如drop update等 after insert asbgein 在上面的操作orders表的ins...

觸發器 迴圈刪除小例子

a表中刪除一條記錄,b表根據a表刪除的記錄中的某個欄位的資料相應刪除相關記錄。觸發器 create or replace trigger deletehrpost after delete on hr post for each row declare pragma autonomous trans...

MySQL儲存過程 觸發器 小例子

一 儲存過程 語法 create procedure in out inout 引數名 資料類形.begin end 引數 in 輸入引數 表示該引數的值必須在呼叫儲存過程的時候指定,在儲存過程中修改該引數的值不能被返回,為預設值。out 輸出引數 該值可在儲存過程內部被改變,並可返回。inout ...