如何循环插入查询数据

accpchenjun 2010-10-28 05:36:46
insert into dbo.Bas_Bulletin(Bul_Type,Bul_Title)values('test','tst2')
如何获取Bas_Bulletin的当前插入ID号并插入Bas_BulletinReader表
查询语句如下
select Psn_ID dbo.Right_UserRight where RR_ID = 'HHAC'
insert into Bas_BulletinReader(Bul_ID,Psn_ID) values(Bas_Bulletin的当前插入ID号,查询语句查出几个人就插入几条数据)
如何循环插入select Psn_ID dbo.Right_UserRight where RR_ID = 'HHAC' 查询数据
...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
abuying 2010-10-29
  • 打赏
  • 举报
回复
使用output
sql server 2005中的output子句,可以使你从修改语句(INSERT、UPDATE、DELETE)中将数据返回到表变量中。带结果的 DML 的有用方案包括清除和存档、消息处理应用程序以及其他方案。这一新的 OUTPUT 子句的语法为:
OUTPUT <dml_select_list> INTO @table_variable

可以通过引用插入的表和删除的表来访问被修改的行的旧/新映像,其方式与访问触发器类似。在 INSERT 语句中,只能访问插入的表。在 DELETE 语句中,只能访问删除的表。在 UPDATE 语句中,可以访问插入的表和删除的表

declare @id int
insert into dbo.Bas_Bulletin(Bul_Type,Bul_Title)values('test','tst2')
output inserted.Bas_Bulletid into @id --就得到新的Bas_Bulletid 赋值给@i
--是不是有点像触发器


详见http://xiaoer-1982.javaeye.com/blog/497473
happy664618843 2010-10-29
  • 打赏
  • 举报
回复
scope_identity();返回最大标识 只作用于当前的表中
@@identity作用于整个数据库 !!!
dawugui 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 accpchenjun 的回复:]
关键是如何循环插入
insert into Bas_BulletinReader(Bul_ID,Psn_ID) values(SELECT IDENT_CURRENT('Bas_Bulletin'),查询语句查出几个人就插入几条数据)
查询语句如下
select Psn_ID dbo.Right_UserRight where RR_ID = 'HHAC'
[/Quote]
如果是自增列,不就是:@@identity?
GuoPingZ 2010-10-29
  • 打赏
  • 举报
回复
insert into Bas_BulletinReader(Bul_ID,Psn_ID) select IDENT_CURRENT('Bas_Bulletin'),Psn_ID from Right_UserRight where RR_ID = 'HHAC'
accpchenjun 2010-10-29
  • 打赏
  • 举报
回复
关键是如何循环插入
insert into Bas_BulletinReader(Bul_ID,Psn_ID) values(SELECT IDENT_CURRENT('Bas_Bulletin'),查询语句查出几个人就插入几条数据)
查询语句如下
select Psn_ID dbo.Right_UserRight where RR_ID = 'HHAC'
「已注销」 2010-10-28
  • 打赏
  • 举报
回复
SELECT  IDENT_CURRENT('Bas_Bulletin')  --返回指定表中生成的最后一个标示值   
rucypli 2010-10-28
  • 打赏
  • 举报
回复
insert into dbo.Bas_Bulletin(Bul_Type,Bul_Title)values('test','tst2')
select @@identity

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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