SQL A数据库导入B数据库

--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
-将如上查询到的语句复制到查询其中执行


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xushugang007

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值