无意之中 实现了个两表互相调用插入触发器的代码

无意之中 实现了个两表互相调用插入触发器的代码

代码如下

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
create table A
(id
int identity(1,1),Namevarchar(50))
create table B
(id
int identity(1,1),Namevarchar(50))
go

CREATETRIGGER ta1ON A
FORinsert
AS
declare @Name varchar(50) ,@a int
select @Name=namefrom inserted
commit
BEGIN TRANSACTION
SELECT @a=count(name)FROM BWHERE name=@name
if(@a=0)begin
insertinto B (Name)values (@Name)
end
go


CREATETRIGGER ta2ON B
FORinsert
AS
declare @Name varchar(50) ,@a int
select @Name=namefrom inserted
SELECT @a=count(name)FROM AWHERE name=@name
if(@a=0)
begin
insertinto A(Name)values (@Name)
commit
BEGIN TRANSACTION
end
go

insert into A(name)values('2')

如不清楚请留言 或登录 http://www.lvtaostudio.com查看和留言

你可能感兴趣的:(Go)