不允許插入 刪除某些表資料的觸發器

2021-05-28 01:25:17 字數 1347 閱讀 7666

插入

use [adventureworks]

gocreate trigger [sales].istore].[store]

after insert as

begin

set nocount on;

begin try

--判斷是否插入記錄的customerid欄位是否已經在[sales].[individua]表中存在相同的記錄

if exists (select * from inserted inner join [sales].[individua]

on inserted.[customerid] = [sales].[individua].[customerid])

begin

--若有相同記錄,則回滾事物

if @@trancount > 0

begin

rollback transaction;

endend;

end try

begin catch

execute [dbo].[uspprinterror];

--呼叫儲存過程,在錯誤日誌表中記錄錯誤產生詳情

if @@transcount > 0

begin

rollback transaction;

end

execute [dbo].[uspprinterror];

end catch;

end ;

go刪除

use [adventureworks]

gocreate trigger [humanresources].[demployee] on [humanresouces].[employee]

instead of delete not for replication as

begin

set nocount on;

declare @deletecount int;

select  @deletecount = count(*) from deleted;

if @deletecount > 0  

begin

raiserror

(n'employees cannot be deleted .they can only be marked as not current .',--message

10,--severity.

1);--state.

--回滾事物

if @@trancount > 0

begin

rollback transaction;

endend;

end;

go  

判斷出乙個表的哪些欄位不允許為空

select column name from information schema.columns where is nullable no and table name admin columns 當前資料庫中當前使用者可以訪問的每乙個列在該檢視中佔一行。information schema.c...

觸發器 當表1插入資料時將表1的資料插入表2

觸發器學習 alter trigger 觸發器名 on 表1 for insert asbegin if select count 1 from 表1 0 print 未插入資料 else insert into 表2 字段 select 對應字段 from inserted end 儲存過程學習 ...

取A表的某些列的資料插入到B表中(複製)

第一步 建立乙個臨時表tmp create table tmp id varvhar 36 not null,memberid varvhar 36 dffault null,shopid varvhar 255 dffault null,primary key id engine innodb d...