2013-11-01 57 views
1

我想使用SQL查詢將所有表從一個數據庫移動到另一個數據庫,並使用主鍵和所有其他鍵 。我正在使用SQL Server 2005,並且我得到了一個SQL查詢來移動表,但密鑰沒有移動。使用主鍵和其他鍵將表從一個數據庫移動到另一個數據庫

而我的疑問是如下

set @cSQL='Select Name from SRCDB.sys.tables where Type=''U''' 

Insert into #TempTable 
exec (@cSQL) 

while((select count(tName) from #t1Table)>0) 
begin 
    select top 1 @cName=tName from #t1Table 
    set @cSQL='Select * into NEWDB.dbo.'[email protected]+' from SRCDB.dbo.'[email protected] +' where 1=2' 
    exec(@cSQL) 
    delete from #t1Table where [email protected] 
end 

其中SRCDB是源數據庫的名稱和NEWDB是目標數據庫的

我怎樣才能達到這個名字..?

誰能幫我在這...

謝謝...

+0

我不要以爲'哪裏1 = 2'會對你有什麼好處。 –

+0

它會複製表沒有數據 – ShaQue

+0

@Upendra:我不想移動表的記錄,所以我作出的條件爲假 – ShaQue

回答

1

以下T-SQL語句將所有表,主鍵和外鍵從一個數據庫移到另一個數據庫。請注意,SELECT * INTO FROM ... WHERE 1 = 2方法不會創建COMPUTED列和用戶數據類型。同時假設所有的主鍵都聚集

--ROLLBACK 
SET XACT_ABORT ON 
BEGIN TRAN 
DECLARE @dsql nvarchar(max) = N'' 
SELECT @dsql += ' SELECT * INTO NEWDB.dbo.' + name + ' FROM SRCDB.dbo. ' + name + ' WHERE 1 = 2' 
FROM sys.tables 
--PRINT @dsql 
EXEC sp_executesql @dsql 

SET @dsql = N'' 
;WITH cte AS 
(SELECT 1 AS orderForExec, table_name, column_name, constraint_name, ordinal_position, 
     'PRIMARY KEY' AS defConst, NULL AS refTable, NULL AS refCol 
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE   
    WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1  
    UNION ALL 
    SELECT 2, t3.table_name, t3.column_name, t1.constraint_name, t3.ordinal_position, 
      'FOREIGN KEY', t2.table_name, t2.column_name 
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as t1 
    JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE t2 ON t1 .UNIQUE_CONSTRAINT_NAME = t2.CONSTRAINT_NAME 
    JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE t3 ON t1.CONSTRAINT_NAME = t3.CONSTRAINT_NAME 
      AND t3.ordinal_position = t2.ordinal_position 
    ) 
    SELECT @dsql += ' ALTER TABLE NEWDB.dbo.' + c1.table_name + 
      ' ADD CONSTRAINT ' + c1.constraint_name + ' ' + c1.defConst + ' (' +   
      STUFF((SELECT ',' + c2.column_name 
        FROM cte c2 
        WHERE c2.constraint_name = c1.constraint_name 
        ORDER BY c2.ordinal_position ASC 
        FOR XML PATH(''), TYPE 
       ).value('.', 'nvarchar(max)'), 1, 1, '') + ')' + 
        CASE WHEN defConst = 'FOREIGN KEY' THEN ' REFERENCES ' + c1.refTable + ' (' + 
      STUFF((SELECT ',' + c2.refCol 
        FROM cte c2 
        WHERE c2.constraint_name = c1.constraint_name 
        ORDER BY c2.ordinal_position ASC 
        FOR XML PATH(''), TYPE 
       ).value('.', 'nvarchar(max)'), 1, 1, '') + ')' ELSE '' END 
    FROM (SELECT DISTINCT orderForExec, table_name, defConst, constraint_name, refTable FROM cte) AS c1 
    ORDER BY orderForExec 
    --PRINT @dsql 
    EXEC sp_executesql @dsql 
COMMIT TRAN 
+0

它工作正常,你可以解釋清楚一下; WITH CTE AS和進程是怎麼回事 – ShaQue

+0

@ShaQue在這種情況下; WITH CTE使用SELECT ... FROM(SELECT ... FROM),換句話說,它是一個子查詢。我認爲它更具可讀性 –

0

您可以生成源數據庫的自定義腳本並運行目標數據庫腳本。

這裏是link和稍好[一] [2]

獲得完整的表,然後執行delete查詢目標數據庫上按規定

如果你想查詢的事情。我想這link將是有益的

+0

謝謝,但我想在Sql查詢 – ShaQue

+0

@ShaQue我已經附加我的上面的答案與一個鏈接演示如何用查詢來完成。 – Dusht

+0

@Dushi:我不知道哪個和所有的表有主鍵,所以我不能在你的方式設置 – ShaQue

0
DECLARE @strSQL NVARCHAR(MAX) 
DECLARE @Name VARCHAR(50) 

SELECT Name into #TempTable FROM SRCDB.sys.tables WHERE Type='U' 

WHILE((SELECT COUNT(Name) FROM #TempTable) > 0) 
BEGIN 
    SELECT TOP 1 @Name = Name FROM #TempTable 
    SET @strSQL = 'SELECT * INTO NEWDB.dbo.[' + @Name + '] FROM SRCDB.dbo.[' + @Name + ']' 
    EXEC(@strSQL) 

    DELETE FROM #TempTable WHERE Name = @Name 
END 

DROP TABLE #TempTable 

如果你有目標表已經創建,然後只設置標識插入上改變查詢象下面這樣:

SET @strSQL = ' SET IDENTITY_INSERT NEWDB.dbo.[' + @Name + '] ON; ' + 
       ' INSERT INTO NEWDB.dbo.[' + @Name + '] SELECT * FROM SRCDB.dbo.[' + @Name + ']' + 
       ' SET IDENTITY_INSERT NEWDB.dbo.[' + @Name + '] OFF ' 

UPDATE:

如果您不想要記錄,只想創建所有密鑰約束表,然後檢查該解決方案:

In SQL Server, how do I generate a CREATE TABLE statement for a given table?

+0

謝謝,它不移動的關鍵,我不能使用更新聲明,因爲有些表沒有鍵 – ShaQue

+0

什麼是意思是不移動鍵?你的意思是表的所有主鍵約束? –

+0

:是的所有的主鍵和標識鍵 – ShaQue

相關問題