SQL Server 動態生成數據庫全部表Insert語句

1、 背景sql

  SQL Server,若是咱們須要把數據庫A的全部表數據到數據庫B中,一般咱們會怎麼作呢?我會使用SSMS的導入導出功能,進行表數據的導入導出,無可厚非,這樣的導入很是簡單和方便;數據庫

  可是,當咱們的表有上百個,並且有些表是有自增ID的,那麼這個時候使用SSMS的話,你須要一個個手動設置(如圖1),你要知道,須要設置上百個的這些選項是件多麼痛苦的事情,並且最後極可能會由於外鍵約束致使導入導出失敗。ide

wpsFA22.tmp

(圖1)spa

  雖然SSMS在導入導出的最後一步提供了生成SSIS包的功能,可是對於轉移數據的需求來講,仍是沒法達到我想要的快速、方便。code

  天然而然,我想到了INSERT INTO XX SELECT FROM XX WHERE這樣的方式(這種方式的好處就是能夠對數據記錄、字段進行控制),可是如何才能快速生成整個數據庫全部表的這些語句呢?blog

  假如你須要批量生成下面的SQL,我想這篇文章就能夠幫到你了:get

--[OpinionList]
SET IDENTITY_INSERT [master_new].[dbo].[OpinionList] ON
INSERT INTO [master_new].[dbo].[OpinionList](Id,Batch,LinkId,DB_Names,CreateTime)
SELECT * FROM [DBA_DB].[dbo].[OpinionList]
SET IDENTITY_INSERT [master_new].[dbo].[OpinionList] OFF
GO

 

2、 腳本解釋string

(一) 我編寫了一個模板,這個模板你只須要設置@fromdb和@todb的名稱,這樣就會生成從@fromdb導出全部表插入到@todb中的SQL語句,須要注意的是:要選擇@fromdb對應的數據庫執行模板SQL,否則沒法生成須要的表和字段。it

DECLARE @fromdb VARCHAR(100)
DECLARE @todb VARCHAR(100)
DECLARE @tablename VARCHAR(100)
DECLARE @columnnames NVARCHAR(300)
DECLARE @isidentity NVARCHAR(30)
DECLARE @temsql NVARCHAR(max)
DECLARE @sql NVARCHAR(max)
SET @fromdb = 'master'
SET @todb = 'master_new'

--遊標
DECLARE @itemCur CURSOR
SET @itemCur = CURSOR FOR 
    SELECT '['+[name]+']' from sys.tables WHERE type='U' order by name

OPEN @itemCur
FETCH NEXT FROM @itemCur INTO @tablename
WHILE @@FETCH_STATUS=0
BEGIN
    
    SET @sql = ''

    --獲取表字段
    SET @temsql = N'
    BEGIN
    SET @columnnamesOUT =''''
    SELECT @columnnamesOUT = @columnnamesOUT + '','' + name 
    From sys.columns where object_id=OBJECT_ID(''['+@fromdb+'].dbo.'+@tablename+''')
    order by column_id
    SELECT @columnnamesOUT=substring(@columnnamesOUT,2,len(@columnnamesOUT))
    END
    '
    EXEC sp_executesql @temsql,N'@columnnamesOUT NVARCHAR(300) OUTPUT',@columnnamesOUT=@columnnames OUTPUT

    PRINT ('--'+@tablename)

    --判斷是否有自增字段
    SET @temsql = N'
    BEGIN
    SET @isidentityOUT =''''
    SELECT @isidentityOUT = name 
    From sys.columns where object_id=OBJECT_ID(''['+@fromdb+'].dbo.'+@tablename+''')
    and is_identity = 1
    END
    '
    EXEC sp_executesql @temsql,N'@isidentityOUT NVARCHAR(30) OUTPUT',@isidentityOUT=@isidentity OUTPUT

    --IDENTITY_INSERT ON
    IF @isidentity != ''
    BEGIN
        SET @sql = 'SET IDENTITY_INSERT ['+@todb+'].[dbo].['+@tablename+'] ON
'
    END

    --INSERT
    SET @sql = @sql+'INSERT INTO ['+@todb+'].[dbo].['+@tablename+']('+@columnnames+')
SELECT * FROM ['+@fromdb+'].[dbo].['+@tablename+']'

    --IDENTITY_INSERT OFF
    IF @isidentity != ''
    BEGIN
        SET @sql = @sql+'
SET IDENTITY_INSERT ['+@todb+'].[dbo].['+@tablename+'] OFF'
    END

    --返回SQL
    PRINT(@sql)PRINT('GO')+CHAR(13)

    FETCH NEXT FROM @itemCur INTO @tablename
END 

CLOSE @itemCur
DEALLOCATE @itemCur

 

(二) 下面就是返回的生成的部分腳本,模板會自動判斷表是否存在自增字段,若是存在就會生成對應的IDENTITY_INSERT語句。io

--spt_values
INSERT INTO [master_new].[dbo].[spt_values](name,number,type,low,high,status)
SELECT * FROM [master].[dbo].[spt_values]
GO

--[OpinionList]
SET IDENTITY_INSERT [master_new].[dbo].[OpinionList] ON
INSERT INTO [master_new].[dbo].[OpinionList](Id,Batch,LinkId,DB_Names,CreateTime)
SELECT * FROM [DBA_DB].[dbo].[OpinionList]
SET IDENTITY_INSERT [master_new].[dbo].[OpinionList] OFF
GO
相關文章
相關標籤/搜索