首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

同时写入两张表并关联ID,该如何处理

2012-02-03 
同时写入两张表并关联ID要将一些信息分别写入两张表A,BB中的ID要等于A中的ID,A中的ID为自增要怎么写比较方

同时写入两张表并关联ID
要将一些信息分别写入两张表A,B
B中的ID要等于A中的ID,A中的ID为自增
要怎么写比较方便?

[解决办法]
declare @returnid int
insert into A ……
select returnid = @@identity
insert into B ……

应该可以一次成功插入两张表。
[解决办法]

SQL code
create table a(   id int primary key identity(1,1),   name varchar(50))create table b(   id int primary key,   name varchar(50))declare @returnID intinsert into a (name) values ('testa')select @returnID=@@identity --获取a中插入的数据的IDinsert into b (id,name) values (@returnID,'testb')select * from aselect * from bdrop table adrop table b 

热点排行