create trigger triA on table2 instead of insert as begin insert table2 select a.* from inserted a join table1 b on a.contract_no=b.contract_no end go第2个类似处理。
注意考虑多条记录的情况:CREATE TRIGGER [tr_insert] ON table2 FOR INSERT AS if exists(select * from (select table1.contract_no from inserted left join table1 on table1.contract_no=inserted.contract_no) t where contract_no is null) RAISERROR ('contract_no不存在!!!',16,1) goCREATE TRIGGER [tr_delete] ON table2 FOR DELETE AS if exists(select * from (select table1.out_date from inserted join table1 on table1.contract_no=inserted.contract_no) t where out_date is not null) RAISERROR ('out_date不为空,不允许删除!!!',16,1) go
if exists(select * from (select table1.contract_no from inserted left join table1 on table1.contract_no=inserted.contract_no) t where contract_no is null) RAISERROR ('contract_no不存在!!!',16,1)这点考虑不错,比较周到!
FOR INSERT
AS
if exists(select * from (select table1.contract_no from inserted left join table1 on table1.contract_no=inserted.contract_no) t where contract_no is null)
RAISERROR ('contract_no不存在!!!',16,1)
goCREATE TRIGGER [tr_delete] ON table2
FOR DELETE
AS
if exists(select * from (select table1.out_date from inserted join table1 on table1.contract_no=inserted.contract_no) t where out_date is not null)
RAISERROR ('out_date不为空,不允许删除!!!',16,1)
go
RAISERROR ('contract_no不存在!!!',16,1)这点考虑不错,比较周到!