在SQL中向多个数据库中插入相同的记录?

torrent2008 2004-07-28 10:50:57
想通过游标的方式来控制
标位置数据库的名称
select name from master.dbo.sysdatabases where name like 'zw%'
zw0001
zw0002
zw0003
zw0004
.......

来获取数据库名称列表
---
插入的记录为
insert into gl_gnqx (userid,gnid)
values('2','471')

用T-SQL语句写
XX
...全文
152 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunriver 2004-07-29
  • 打赏
  • 举报
回复
while @@FETCH_STATUS = 0
是指还有数据可取,读到最后一条时@@FETCH_STATUS = -1

declare @db varchar(50)
变量在使用前定义即可
torrent2008 2004-07-29
  • 打赏
  • 举报
回复
ding
----
可以帮我回答如上问题吗/
pbsql 2004-07-28
  • 打赏
  • 举报
回复
看1楼的
torrent2008 2004-07-28
  • 打赏
  • 举报
回复
ding
torrent2008 2004-07-28
  • 打赏
  • 举报
回复
declare @db varchar(50)
声明变量一定要在游标之前吗/

open c_database
fetch next from c_database into @name
fetch next from c_database into @name
要输入2遍吗/

while @@FETCH_STATUS = 0
什么意思/
pjjia 2004-07-28
  • 打赏
  • 举报
回复
----声明游标
declare @name varchar(40)
declare c_database cursor
for select name from master.dbo.sysdatabases where name like 'zw%'

---打开
open c_database
fetch next from c_database into @name
fetch next from c_database into @name
select @name = @name+'.dbo.gl_gnqx'
insert into @name (userid,gnid) values('2','471')

while @@FETCH_STATUS = 0
begin
fetch next from c_database into @name
select @name = @name+'.dbo.gl_gnqx'
insert into @name (userid,gnid) values('2','471')
end
--
close c_databse
deallocate c_database
go
pbsql 2004-07-28
  • 打赏
  • 举报
回复
declare @db varchar(50)
declare c cursor for
select name from master.dbo.sysdatabases where name like 'zw%'
open c
fetch next from c into @db
while @@fetch_status=0
begin
exec('use '+@db
+' insert into gl_gnqx (userid,gnid) values(''2'',''471'')')
fetch next from c into @db
end
close c
deallocate c

34,873

社区成员

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

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