• 将一数据库中的所有自己建的表的字段融合到一张大表里


    --功能:将一数据库中的所有自己建的表的字段融合到一张大表里。
    --时间:2009.03.19

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

    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

  • 相关阅读:
    【Vue优化】—— Vue项目上线可以做的一些基本优化
    从debian10(buster) 升级到 11 (bullseye)
    jeecgboot集成seata实战
    免费的可视化Web报表工具,JimuReport v1.5.0beta版本发布
    制作报表原来可以这么简单—积木报表使用分享
    如何实现快速高效开发?低代码平台jeecgboot完美解决—jeecgboot3.1新特性
    比excel更好用的免费拖拽报表—JimuReport 1.4.4新特性
    制作打印报表费时费力?积木报表帮你轻松搞定—医院体检项目实战
    Asp.Net Core WebApi入门
    .Net5学习基于.Net5创建WebApi项目
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1416724.html
Copyright © 2020-2023  润新知