--A数据库导入B数据库
--将db_A和db_B更换为实际数据库名
use db_A
go
--建立临时表
if object_id('tbcol_temp') is not null
drop table tbcol_temp
select b1.name as tb_name,a1.name as clm_name into db_B.dbo.tbcol_temp from db_A.dbo.syscolumns a1 inner join db_A.dbo.sysobjects b1 on a1.id = b1.id where columnproperty(a1.id, a1.name, 'IsIdentity') <> 1
use db_B
go
declare @tbname as nvarchar(500),@clmname as nvarchar(500),@sql as nvarchar(1000)
select @sql = ''
--建立游标
declare tb_cur cursor for
select name from sysobjects where type = 'U' and name in (select tb_name from tbcol_temp)
open tb_cur
fetch tb_cur into @tbname
while @@fetch_status = 0
begin
declare clm_cur cursor for
select a.name from syscolumns a inner join sysobjects b on a.id = b.id where (columnproperty(a.id, a.name, 'IsIdentity') <> 1) and (b.name = @tbname) and (a.name in (select clm_name from tbcol_temp where tb_name = @tbname))
open clm_cur
fetch clm_cur into @clmname
while @@fetch_status = 0
begin
set @sql = @sql + @clmname + ','
fetch clm_cur into @clmname
end
close clm_cur
deallocate clm_cur
print 'TRUNCATE TABLE ' + @tbname
print 'ALTER TABLE ' + @tbname + ' disable TRIGGER all'
print replace('insert into ' + @tbname + ' (' + @sql + ' from)',', from','') + replace('select ' + @sql + ' from db_A.dbo.' + @tbname,', from',' from')
print 'ALTER TABLE ' + @tbname + ' enable TRIGGER all'
print 'go'
set @sql = ''
fetch tb_cur into @tbname
end
close tb_cur
deallocate tb_cur
--删除临时表
if object_id('tbcol_temp') is not null
drop table tbcol_temp
go
-将如上查询到的语句复制到查询其中执行