表a(id int,name varchar(50))
表b (id int ,mane varchar(50))
------------------------
在表a中插入一条记录,插入前检查表b的name字段中是否存在表a刚要被插入的name值,如果存在就允许插入,如果表B中不存在则不允许插入。
如表b name字段值如下
mane
A
B
C则insert a values('1','A') //正确
insert a values('2','D')//不能被插入,因为表b的name字段中没有D.
表b (id int ,mane varchar(50))
------------------------
在表a中插入一条记录,插入前检查表b的name字段中是否存在表a刚要被插入的name值,如果存在就允许插入,如果表B中不存在则不允许插入。
如表b name字段值如下
mane
A
B
C则insert a values('1','A') //正确
insert a values('2','D')//不能被插入,因为表b的name字段中没有D.
建立表关系
b为主表
a为子表
a.name与b.name外键
当b.name没那值时a.name插入失败
这样和循环有什么关系?
INSERT AS
begin
--RAISERROR (50009, 16, 10)
declare @count int
select @count = count(*) from b where name = (select name from INSERTED )
if(@count=0)
--delete a where id=(select id from INSERTED)
delete a where id=@@identity--如果id是递增列就用这种方式 不是就用上一种
endinsert into a values('aa')
select * from a
INSERT AS
begin
--RAISERROR (50009, 16, 10)
declare @count int
select @count = count(*) from b where name = (select name from INSERTED )
if(@count=0)
--delete a where id=(select id from INSERTED)
delete a where id=@@identity--如果id是递增列就用这种方式 不是就用上一种
endinsert into a values('aa')
select * from a
if not exists(select * from b where [name]= @name)
Begin
--insert into a(ID,Iname) values(@id,@name)
End