注册 登录
编程论坛 SQL Server论坛

游标处理数据导入导出的问题(包括含有自增字段的表)

初学Delphi 发布于 2007-05-28 13:59, 1447 次点击

declare @B varchar(50) --新库名称
set @B='xinku'--新的数据库名称
declare @i int
set @i=0
declare @j int
select @j=count(name) from sysobjects where xtype='u' and name<>'dtproperties'
declare @a varchar(50)
declare 游标 scroll cursor
for
select name from sysobjects where xtype='u' and name<>'dtproperties'
open 游标
fetch first from 游标
declare @t table(id int identity(1,1),name varchar(50))
--下面的是数据库中的含有自增字段的表,可根据实际情况自己修改
insert into @t(name) select 'BiaoZhun' union select 'BiaoZhun1' union select 'CaiPinYongLiao' union
select 'CangKu' union select 'ChuKu' union select 'DaYinJiGuanLi' union select 'DengLuMingXi' union
select 'GongYingShang' union select 'JiaoBanJiLu' union select 'JiLiangDanWeiBiao' union select 'KuCun' union
select 'KuCunZhangHao' union select 'TaiWei' union select 'TuiCai' union select 'YuanCaiLiao' union
select 'zhuantaijilu' union select 'YuanCaiLiaoLeiBie' union select 'TuiCaiMingXi'
while @@FETCH_STATUS=0
begin
set @i=@i+1
if(@i>=@j)
break
fetch next from 游标 into @a
select * from @t where name =''+@a+''
if @@rowcount=0
begin
exec ('insert into '+@B+'..'+@a+' select * from '+@a)
end
else
begin
declare @lie varchar(200)
set @lie='bianhao'
--需要修改自增字段的
declare 游标1 scroll cursor
for
select name from syscolumns where id=object_id(''+@a+'')
open 游标1
declare @lie1 varchar(50)
fetch first from 游标1
while @@FETCH_STATUS=0
begin
fetch next from 游标1 into @lie1
set @lie=@lie+','+@lie1
end
set @lie=substring(@lie,1,len(@lie)-charindex(',', reverse(@lie)))
fetch first from 游标1
close 游标1
deallocate 游标1
exec ('set identity_insert '+@B+'..'+@a+' on insert into '+@B+'..'+@a+'('+@lie+') select * from '+@a+' set identity_insert '+@B+'..'+@a+' off')
end
end
close 游标
deallocate 游标

上面的是创建好数据库'xinku'之后并且表已经创建好

估计可以换个方法select * into xinku..table from table
没有测试过,可以自己测试一下

9 回复
#2
初学Delphi2007-05-29 13:19

难道没人遇到这问题么?
我在这边就遇到过,
很多时候不是说在企业管理器里导入导出或者用backup restore 以及附加就可以的
没有人顶 真是的

#3
棉花糖ONE2007-05-29 14:00

顶顶顶

#4
棉花糖ONE2007-05-29 14:03
select * into xinku..table from table 这个可以,自增字段导过去还是自增的,前几天才发现
#5
棉花糖ONE2007-05-29 14:21
exec sp_msforeachtable "select * into data2.? from ?"
试试我这个,data2是个数据库
#6
初学Delphi2007-06-12 11:47
早不说,解决问题了你才发出来
#7
大连乖乖猫2007-06-14 15:35
狗屁~!!懂不懂啊
#8
初学Delphi2007-06-14 15:44

楼上的?哪里不行可以直接说,没必要这样吧?我这边测试可以,你那边就是狗屁?敢问你那边狗屁是怎么显示出来的?

#9
大连乖乖猫2007-06-15 08:41
你丫的 在哪工作现在
#10
初学Delphi2007-06-15 14:38
小刘吧?回完帖子才看你的资料..
实在抱歉,还让您替我挂牵着工作,我现在还没工作.
上面的例子在我这边测试可以了,那是我临走给贾飞写的一个脚本.
你也不说说在你那里运行出现什么错误?
可能我写的脚本认人吧,不过没关系,我写的这狗屁不看也罢

再有每个人做事都是有原因的.当初我留下来有我的原因,有些话我不需要重复第2遍.
1