现有如下的表
create table temp(
name1 varchar(30) not null,
name2 varchar(30) not null
)
我希望执行如下语句时
insert into temp('aa','bb')
insert into temp('aa','cc')
insert into temp('bb','aa')
会提示第三句出错,提示违反特殊唯一约束
特殊唯一约束的定义:一般地,两个字段组合的唯一约束,会认为
insert into temp('aa','bb')
insert into temp('bb','aa')
是两个不同的数据项,而这个我自己定义的特殊唯一约束,是会认为
insert into temp('aa','bb')
insert into temp('bb','aa')
是同一数据项请问我该如何定义实现该功能的触发器呢?
我自己写的触发器
create trigger test for insert
as
begin
if(select count(*)
from temp,inserted as t2
where temp.name1=t2.name2
and temp.name2=t2.name1)>0
begin
rollback transaction
end
end
但是如何我怎么插入数据,都报错
create table temp(
name1 varchar(30) not null,
name2 varchar(30) not null
)
我希望执行如下语句时
insert into temp('aa','bb')
insert into temp('aa','cc')
insert into temp('bb','aa')
会提示第三句出错,提示违反特殊唯一约束
特殊唯一约束的定义:一般地,两个字段组合的唯一约束,会认为
insert into temp('aa','bb')
insert into temp('bb','aa')
是两个不同的数据项,而这个我自己定义的特殊唯一约束,是会认为
insert into temp('aa','bb')
insert into temp('bb','aa')
是同一数据项请问我该如何定义实现该功能的触发器呢?
我自己写的触发器
create trigger test for insert
as
begin
if(select count(*)
from temp,inserted as t2
where temp.name1=t2.name2
and temp.name2=t2.name1)>0
begin
rollback transaction
end
end
但是如何我怎么插入数据,都报错
改成
create trigger test on temp for insert
这是我打漏而已,不影响我的问题
create trigger tgr_t1
on t1 instead of insert
as
begin
if (1=1)
print 'aa' ; --要不你用一句什么也不做代替,把after 改成 instead of
else
insert into t1 select 100,200,11;
end