业务问题

mingxin 2012-10-16 08:49:15
有业务如下:
A是总表,B是明细表
往A,B表同时插入数据。A表中有个自增列ID值,需要从A表中得到ID,再插入B表中

请问高手,用事务如何处理这种业务?
...全文
201 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
相信你行 2012-12-19
  • 打赏
  • 举报
回复
用存储过程,最把插入的ID值返回就得到了
mingxin 2012-10-16
  • 打赏
  • 举报
回复
等待。。。
mingxin 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
7楼的如果业务比较简单可以,但我需要在前台处理,不在后台处理
==>
你在前台,查询 @@identity 就可以得到总表,刚插入的id
[/Quote]

用事务
csdn_风中雪狼 2012-10-16
  • 打赏
  • 举报
回复
7楼的如果业务比较简单可以,但我需要在前台处理,不在后台处理
==>
你在前台,查询 @@identity 就可以得到总表,刚插入的id
mingxin 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
SQL code


declare @a table
(
id int IDENTITY(1,1),
name varchar(20)
);
declare @detail table
(
id int,
detailId int
);

--插入过程执行
declare @temp table
(
tempid in……
[/Quote]

7楼的如果业务比较简单可以,但我需要在前台处理,不在后台处理
kensouterry1 2012-10-16
  • 打赏
  • 举报
回复


declare @a table
(
id int IDENTITY(1,1),
name varchar(20)
);
declare @detail table
(
id int,
detailId int
);


--插入过程执行
declare @temp table
(
tempid int
);
insert into @a --插入表a并输出id
OUTPUT inserted.id
INTO @temp
values('种类1')

insert into @detail --插入明细表 @detail
select (select top(1) tempid from @temp), 1 union all
select (select top(1) tempid from @temp), 2 union all
select (select top(1) tempid from @temp), 3

--测试明细表数据
select * from @detail

mingxin 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
如果A表插入成功,返回A表的@@identity,这个@@identity就是B表所需的ID
[/Quote]

如果不用事务就可以,用事务就不行
cdefg198 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

如果A表插入成功,返回A表的@@identity,这个@@identity就是B表所需的ID
[/Quote]
SQLserver数据库就可以使用这个方法
Pigeon汪 2012-10-16
  • 打赏
  • 举报
回复
如果A表插入成功,返回A表的@@identity,这个@@identity就是B表所需的ID
mingxin 2012-10-16
  • 打赏
  • 举报
回复
2楼能不能说的详细点
sj490790083 2012-10-16
  • 打赏
  • 举报
回复
给A表添加一个after插入触发器
feng84131421 2012-10-16
  • 打赏
  • 举报
回复
自增ID,不是业务採番取得ID?
mingxin 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
有业务如下:
A是总表,B是明细表
往A,B表同时插入数据。A表中有个自增列ID值,需要从A表中得到ID,再插入B表中

请问高手,用事务如何处理这种业务?
[/Quote]

求指导

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧