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

--功能:将一数据库中的所有自己建的表的字段融合到一张大表里。
--时间: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

你可能感兴趣的:(数据库)