首页 >数据库 >mysql教程 >结构相同的数据库之间复制数据

结构相同的数据库之间复制数据

WBOY
WBOY原创
2016-06-07 14:57:231032浏览

结构相同的数据库之间复制数据 无 use oldgoDECLARE NoCheckConstraintAll CURSOR READ_ONLY FOR SELECT sst.name, Schema_name(sst.schema_id) FROM sys.tables sst WHERE sst.TYPE = 'U' DECLARE @name VARCHAR(80), @schema VARCHAR(40)OPEN NoCheckConstr

结构相同的数据库之间复制数据
use old
go
DECLARE NoCheckConstraintAll CURSOR READ_ONLY FOR 
  SELECT sst.name, 
         Schema_name(sst.schema_id) 
  FROM   sys.tables sst 
  WHERE  sst.TYPE = 'U' 
DECLARE @name   VARCHAR(80), 
        @schema VARCHAR(40)

OPEN NoCheckConstraintAll

FETCH NEXT FROM NoCheckConstraintAll INTO @name, @schema

WHILE ( @@FETCH_STATUS <> -1 ) 
  BEGIN 
      IF ( @@FETCH_STATUS <> -2 ) 
        BEGIN 
                DECLARE @sql NVARCHAR(1024) 
SET @sql=' ALTER TABLE ' + Quotename(@schema) 
                           + 
                           '.' + Quotename(@name)+' NOCHECK CONSTRAINT ALL'
                  EXEC Sp_executesql @sql 
        END

      FETCH NEXT FROM NoCheckConstraintAll INTO @name, @schema 
  END

CLOSE NoCheckConstraintAll

DEALLOCATE NoCheckConstraintAll

GO

DECLARE CopyTables CURSOR READ_ONLY FOR 
  SELECT sst.name, 
         Schema_name(sst.schema_id) 
  FROM   sys.tables sst 
  WHERE  sst.TYPE = 'U' 
DECLARE @name   VARCHAR(80), 
        @schema VARCHAR(40)

OPEN CopyTables

FETCH NEXT FROM CopyTables INTO @name, @schema

WHILE ( @@FETCH_STATUS <> -1 ) 
  BEGIN 
      IF ( @@FETCH_STATUS <> -2 ) 
        BEGIN 
                DECLARE @sql NVARCHAR(1024) 
SET @sql='INSERT INTO [new].' + Quotename(@schema) 
                           + 
                           '.' + Quotename(@name)+' SELECT * FROM '+ Quotename(@schema) 
                           + 
                           '.' + Quotename(@name)
                  EXEC Sp_executesql @sql 
        END

      FETCH NEXT FROM CopyTables INTO @name, @schema 
  END

CLOSE CopyTables

DEALLOCATE CopyTables

GO
DECLARE CheckConstraintAll CURSOR READ_ONLY FOR 
  SELECT sst.name, 
         Schema_name(sst.schema_id) 
  FROM   sys.tables sst 
  WHERE  sst.TYPE = 'U' 
DECLARE @name   VARCHAR(80), 
        @schema VARCHAR(40)

OPEN CheckConstraintAll

FETCH NEXT FROM CheckConstraintAll INTO @name, @schema

WHILE ( @@FETCH_STATUS <> -1 ) 
  BEGIN 
      IF ( @@FETCH_STATUS <> -2 ) 
        BEGIN 
                DECLARE @sql NVARCHAR(1024) 
SET @sql=' ALTER TABLE ' + Quotename(@schema) 
                           + 
                           '.' + Quotename(@name)+' CHECK CONSTRAINT ALL'
                  EXEC Sp_executesql @sql 
        END

      FETCH NEXT FROM CheckConstraintAll INTO @name, @schema 
  END

CLOSE CheckConstraintAll

DEALLOCATE CheckConstraintAll

GO
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn