SQLSERVER 匯入帶IDENTITY屬性的資料

來源:互聯網
上載者:User
SQLSERVER 內建的資料匯入工具有兩點不好:
1. 資料庫複寫,會複製所有表的所有資料,有的大表沒有必要導,不好控制
2. 資料匯入,當表中有IDENTITY限制時,只能一個一個表去設定取消,匯入後再設定回去
今天寫了一段SQL,來執行部分表的資料匯入
用到了一些知識點:
1. 建立連結的伺服器
2. 設定IDENTITY列的可插入屬性
3. 帶參數的遊標

建立連結的伺服器
EXEC  sp_addlinkedserver
      @server='DBVIP',
      @srvproduct='',
      @provider='SQLOLEDB',
      @datasrc='58.1.2.3' 

EXEC sp_addlinkedsrvlogin
     'DBVIP',
     'false',
     NULL,
     'sa',
     'sapwd'

// 先產生來來源資料庫的指令碼,在目標伺服器上建立新的資料庫,這是目標資料庫
的結構和來來源資料庫是一樣的

// 先建立一個暫存資料表,將包含IDENTITY屬性列的表的名稱匯入導暫存資料表
// 依據COLSTAT=1來判斷為IDENTITY列不一定準確,沒有查過資料
create table table_tmp (table_name nvarchar(50))
insert into table_temp
select name from sysobjects  where id in (
select id from syscolumns
where id in (select id from sysobjects
where type='u') and colstat=1 ) order by name

//定義一個遊標,獲得所有要導資料的表
declare cur_tab cursor for select table_name from table_tmp
declare @name nvarchar(40)
declare @sql nvarchar(2000)
begin
open cur_tab;
fetch cur_tab into @name;
while(@@fetch_status=0)
begin
// 將表的IDENTITY_INSERT屬性設定為ON,準備匯入資料
    set @sql='set identity_insert '+ @name + ' on;';
    print(@sql);
    --exec(@sql);
   // 這裡聲明帶參數的遊標,取得當前表的所有列,因為表具有IDENTITY的列時,
  // 必須要將表的列全部寫出來啊,才可以匯入
    exec('
    declare cur_col cursor
        for select name from syscolumns
        where id= (
        select id from sysobjects
        where type=''u''
        and name='''+ @name +''')');
        declare @col nvarchar(20);
        declare @in_sql nvarchar(2000);
        declare @in_col nvarchar(1000);
        set @in_col='';
        open cur_col ;
        fetch cur_col into @col;
        while(@@fetch_status=0)
        begin
            set @in_col=@in_col+@col+',';
            fetch cur_col into @col;
        end;
        set @in_col=substring(@in_col,1,len(@in_col)-1);
        set @in_sql='insert into '+@name +' ('+@in_col+') select '+@in_col+ ' from DBVIP.src_db.dbo.'+@name;
        print(@in_sql);
        --exec(@in_sql);
    close cur_col;
    deallocate cur_col   
    set @sql='set identity_insert '+ @name + ' off;';
    print(@sql);
    --exec(@sql);
    fetch cur_tab into @name;
end;
close cur_tab;
deallocate cur_tab
end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.