• 经验:多表复制(结构、数据)


     

    1.结构复制:

    下面的存储过程是将某数据库中的所有用户表的字段提取出来,去掉重复字段,然后用筛选后的字段创建一张新表。

    ALTER PROCEDURE [dbo].[UP_CreateTemp]

    as

    begin

        --1.判断表是否存在

        IF  not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Temp]') AND type in (N'U'))

           begin

               --2.如果表不存在,创建表

               CREATE TABLE [dbo].[Temp](

                  [TempID] [int] IDENTITY(1,1) NOT NULL,

                CONSTRAINT [PK_Temp] PRIMARY KEY CLUSTERED

               (

                  [TempID] ASC

               )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]

               ) ON [PRIMARY]

     

           end

        --

        declare @ColumnCount int

    --3.判断临时表是否存在注意:临时表都存放在[系统数据库的:tempdb数据库]

        if(object_id('tempdb..#TempColumns ') is not null)

           begin

                drop table #TempColumns

           end

    --4.创建自增长临时表

        SELECT IDENTITY(INT,1,1) AS [NewID], * into #TempColumns

    --5.利用系统视图

    FROM    INFORMATION_SCHEMA.COLUMNS C

        WHERE   TABLE_SCHEMA ='dbo'

        AND  

        EXISTS  (

                  SELECT  Table_Name        

                  FROM    INFORMATION_SCHEMA.TABLES T

                  WHERE   C.Table_Name = T.Table_Name

                  AND     T.Table_Name<>'sysdiagrams'

                )

    --

        select @ColumnCount=count(table_catalog) from #TempColumns

        print @ColumnCount

        --

        declare @script nvarchar(255);

        declare @ColumnName nvarchar(255);

        declare @ColumnType nvarchar(255);

        declare @ColumnMax nvarchar(255);

        declare @ColumnNullable nvarchar(255);

    --6.循环

        while @ColumnCount>0

           begin

               if not exists(

                         select * from #TempColumns as T where T.[NewID]=@ColumnCount and

                         exists(

                                select * from INFORMATION_SCHEMA.COLUMNS C

                                           where c.Table_name='temp'and c.Column_Name=T.Column_Name

                                )

                       )

                  begin

                      select @ColumnName=T.Column_Name, @ColumnType=Data_Type,@ColumnMax=character_Maximum_Length, @ColumnNullable=IS_Nullable from #TempColumns as T where T.[newid]=@ColumnCount

                      if @ColumnType='nvarchar'

                         begin

                             set @ColumnType=@ColumnType+'('+@ColumnMax+')';

                         end

                      if @ColumnNullable='yes'

                         begin

                             set @ColumnNullable='null';

                         end

                      else

                         begin

                             set @ColumnNullable='not null';

                         end

     

                      set @script=' ';

                      set @script=@script+'alter table [dbo].[Temp]';

                      set @script=@script+' add '+' '+@ColumnName+' '+@ColumnType+' '+@ColumnNullable;

    --7.执行拼接的语句注意exec(@script),而exec @script 将会出错

                      exec(@script)

                      print @ColumnCount;

                  end

               set @ColumnCount=@ColumnCount-1;

           end

     

    end

     

    2.数据复制:

     下面的存储过程是通过视图将数据复制到新表里。

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

     

     

    ALTER proc [dbo].[UP_CopyTableFromView]

    (

        @sourceViewName nvarchar(50),

        @newTableName nvarchar(50),

        @copySuccess bit output

    )

    as

    begin

     

        declare @script nvarchar(500);

     

        --判断视图是否存在

        IF  not EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID('[dbo].['+@sourceViewName+']') AND type in (N'V'))

           begin

               set @copySuccess=2;

               return ;

           end

     

        --判断表是否存在

        IF  not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].['+@newTableName+']') AND type in (N'U'))

           begin

               begin try

     

                  set @script='select * into '+ isnull(@newTableName,'')+' from '+isnull(@sourceViewName,'');

                  print @script;

     

                  begin tran

                      exec (@script);

                  commit tran

     

                  --

                  set @copySuccess=0;

               end try

               begin catch

                  set @copySuccess=1;

               end catch

           end

     

    end

     思路:如果将存储过程写的比较活,即不管数据表的结构改动,都能正确复制数据,这种方法实现起来比较难。

          所以转换了一下方法,通过视图我们可以很简单、很方便得确定新表中的字段,数据的复制也就比较简单了。

  • 相关阅读:
    4K
    4J
    4C
    I2C总线的仲裁机制
    Linux C中strcpy , strncpy , strlcpy 的区别
    Linux下的USB总线驱动(一)
    C/C++ 语言中的表达式求值
    const变量通过指针修改问题
    关于协议栈XDATA,内存溢出的小结
    Ubuntu安装ssh,及失败解决方案
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1431903.html
Copyright © 2020-2023  润新知