表A
sno
001表B
sno name addr id
001 a 123456 1
001 b 123 2
001 a 123456 3表A中sno是主键,表B中sno、name、id是联合主键。其中id是自增长列。现在要这样的效果向A、B表插入数据,如果在A表中的sno中的记录不存在,则同时向A、B插入数据。如果判断到插入的数据已在A中存在,则A、B表都不插入数据。
是用存储过程还是触发器?具体该怎样实现?我想了好久也还是没写出来。。
sno
001表B
sno name addr id
001 a 123456 1
001 b 123 2
001 a 123456 3表A中sno是主键,表B中sno、name、id是联合主键。其中id是自增长列。现在要这样的效果向A、B表插入数据,如果在A表中的sno中的记录不存在,则同时向A、B插入数据。如果判断到插入的数据已在A中存在,则A、B表都不插入数据。
是用存储过程还是触发器?具体该怎样实现?我想了好久也还是没写出来。。
if object_id('B') is not null drop table B;create table A(sno nvarchar(10));create table B(
sno nvarchar(10),
[name] nvarchar(10),
addr nvarchar(20),
id int identity(1,1))insert into A select '001';insert into B
select '001','a','123456' union all
select '001','b','123' union all
select '001','a','123456'declare @sno nvarchar(10)set @sno = '002'if(select count(*)
from A
where sno = @sno)=0
begin
insert into A select @sno
insert into B select @sno,'a','34567'
end
else
begin
print(@sno + ' exists in table A')
end--drop table a
--drop table b
http://blog.csdn.net/fredrickhu/archive/2009/10/21/4708906.aspx看看 参考一下