---
title: "无意之中 实现了个两表互相调用插入触发器的代码"
author: "wumingwei-123"
date: "2008-09-02T23:58:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2008-09-02T23:58:00Z"
id: 496864
url: "https://wumingwei.iteye.com/blog/496864"
---

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

代码如下

&lt;!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --&gt;  
create table A  
(idint identity(1,1),Namevarchar(50))  
create table B  
(idint 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查看和留言
```