>데이터 베이스 >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으로 문의하세요.
이전 기사:mysql关联表主键重刷다음 기사:select 嵌套查询