insert 触发器
本帖最后由 smilewmr 于 2013-06-05 20:19:16 编辑 说明:
现有表A:字段为 a,b,c,d,e;表B:字段为b,c,d;要做触发器,当表A中有新增记录时,判断各个字段是否合法,合法则插入A表,同时将此记录插入B表,当B表新增完成后,将A表中的记录删除;如果字段有一个不合法则A表不新增记录,B表也不新增,记录直接丢弃。这个触发器如何写呢?在线等。。。。。。 insert?触发器 sql2005
[解决办法]
create table A
(
a int,
b int,
c int,
d int,
e int
);
create table B
(
b int,
c int,
d int
);
CREATE TRIGGER TRI_A ON A
AFTER INSERT
AS
BEGIN
declare @a int,@b int ,@c int ,@d int ,@e int
select @a=a ,@b=b,@c=c ,@d=d,@e=e from inserted i
if @a <>'' --invalid check ,u would write yours business function to replace it
begin
insert into B(b,c,d) select @b,@c,@d
delete a
from A as a,inserted i
where a.a=i.a and a.b=i.b
end else begin
rollback
end
END
[解决办法]
敢问何谓合法?何谓不合法?