使用主键和其他键将表从一个数据库移动到另一个数据库

时间:2013-11-01 10:59:08

标签: sql sql-server-2005

我想使用主键和所有其他键将所有表从一个数据库移动到另一个数据库 使用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.'+@cName+' from   SRCDB.dbo.'+@cName +' where 1=2'
    exec(@cSQL)
    delete from #t1Table where tName=@cName
end

其中SRCDB是源数据库的名称,NEWDB是目标数据库的名称

我怎样才能做到这一点..?

任何人都可以帮助我...

谢谢...

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以生成源数据库的自定义脚本并运行目标数据库的脚本。

这是link,稍微好一点[2]

获取完整表,然后根据需要对目标数据库执行delete查询

如果你想使用Query。我想这link会有所帮助

答案 2 :(得分: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 '

更新:

如果您不想要记录并且只想创建包含所有关键值的表,请检查此解决方案:

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

答案 3 :(得分:0)

以下脚本将源数据库中的许多表复制到另一个目标数据库中,同时考虑到其中一些表具有自动增量列: http://sqlhint.com/sqlserver/copy-tables-auto-increment-into-separate-database