在动态查询中创建表

时间:2019-05-08 06:42:12

标签: sql-server dynamic-sql

我有一个动态查询,它用两个字段创建了一个表。第一个字段来自#Table,第二个字段(CreateDate)以静态方式添加。

if object_id(N'tempdb..#Table') is not null
    drop table #Table;

    create table #Table
    (

        [SchemaName] nvarchar(100) not null
        ,[TableName] nvarchar(128) not null
        ,[FieldId] int  
        ,[ColumnName] nvarchar(100)
        ,FieldDefinitionID int
        ,Ordinal int 
        ,[Data Type] nvarchar(100)
        ,IsNullable bit
        ,IsIdentity bit
        ,HasDefault bit 
        ,DefaultName nvarchar(100)
        ,DefaultDefinition  nvarchar(100)
        ,[Description] nvarchar(100) 
        ,HasCheckConstraint bit  
        ,CheckConstraintName bit 
        ,CheckConstraintDefinition bit 
        ,PartitionIndexKey bit 
        ,ObjectID int 


    );



    insert into #Table
                (
                    [SchemaName] 
                    ,[TableName] 
                    ,[FieldId]   
                    ,[ColumnName]   
                    ,Ordinal 
                    ,[Data Type] 
                    ,IsNullable 
                    ,IsIdentity
                    ,HasDefault 
                    ,HasCheckConstraint
                )
                select 
                N'dbo'
                ,N'test'
                ,-6
                ,N'RecordId'
                ,1
                ,N'int'
                ,0
                ,0
                ,0
                ,0






    insert into #Table
                (
                    [SchemaName] 
                    ,[TableName] 
                    ,[FieldId]   
                    ,[ColumnName]   
                    ,Ordinal 
                    ,[Data Type] 
                    ,IsNullable 
                    ,IsIdentity
                    ,HasDefault 
                    ,HasCheckConstraint
                )
                select 
                N'dbo'
                ,N'test'
                ,-6
                ,N'RecordId'
                ,1
                ,N'int'
                ,0
                ,0
                ,0
                ,0






select          N'

create table [' + [t].[SchemaName] + N'].[' + [t].[TableName] + N']
(
    [' 
    + [t].[ColumnName] 
    + N'] ' 
    + [t].[Data Type] 
    + case
        when [t].[IsNullable] = 0
        then N' not null '
        else N' null '
    end
    + case
        when [t].[IsIdentity] = 1
        then N' identity '
        else N''
    end
    + case
        when [t].[HasDefault] = 1
        then N' constraint [' + [t].[DefaultName] + N'] default ' + [t].[DefaultDefinition]
        else N''
    end
    +
    N'
    ,[CreateDate] datetime not null
);

exec sys.sp_addextendedproperty @name = N''EntityId''
                                ,@value = N''' + cast(11 as nvarchar(max)) + N'''
                                ,@level0type = N''Schema'', @level0name = N''' + [t].[SchemaName] + N'''
                                ,@level1type = N''Table'', @level1name = N''' + [t].[TableName] + N''';

exec sys.sp_addextendedproperty @name = N''MessageId''
                                ,@value = N''' + cast(3456 as nvarchar(max)) + N'''
                                ,@level0type = N''Schema'', @level0name = N''' + [t].[SchemaName] + N'''
                                ,@level1type = N''Table'', @level1name = N''' + [t].[TableName] + N''';

' 
from  #Table t

所以结果就是这个创建语句:

 create table [dbo].[test]
    (
        [RecordId] int not null 
        ,[CreateDate] datetime not null
    );

    exec sys.sp_addextendedproperty @name = N'EntityId'
                                    ,@value = N'11'
                                    ,@level0type = N'Schema', @level0name = N'dbo'
                                    ,@level1type = N'Table', @level1name = N'test';

    exec sys.sp_addextendedproperty @name = N'MessageId'
                                    ,@value = N'3456'
                                    ,@level0type = N'Schema', @level0name = N'dbo'
                                    ,@level1type = N'Table', @level1name = N'test';

我希望得到相同的结果,但是我需要像第一个字段一样以动态方式从#Table获取第二个字段。但是我不知道该怎么办。

2 个答案:

答案 0 :(得分:0)

在执行查询之前,将包含新字段数据的记录添加到#Table

类似这样的东西:

INSERT INTO #Table ([SchemaName], [TableName], [ColumnName], [Data Type])
VALUES ('dbo', 'Test', 'MyNewField', 'MyNewFieldType');

您需要为字段名称及其数据类型填写正确的值。 (可选)您可能还需要为可空性,身份,默认值等设置显式值。

但是,您尚未指定#Table的结构。我从您的查询派生了这些字段,但是可能是这样,您还需要包括其他字段。

答案 1 :(得分:0)

请尝试以下操作:

create table #Table
(
    [SchemaName] nvarchar(100) not null
    ,[TableName] nvarchar(128) not null
    ,[FieldId] int  
    ,[ColumnName] nvarchar(100)
    ,FieldDefinitionID int
    ,Ordinal int 
    ,[Data Type] nvarchar(100)
    ,IsNullable bit
    ,IsIdentity bit
    ,HasDefault bit 
    ,DefaultName nvarchar(100)
    ,DefaultDefinition  nvarchar(100)
    ,[Description] nvarchar(100) 
    ,HasCheckConstraint bit  
    ,CheckConstraintName bit 
    ,CheckConstraintDefinition bit 
    ,PartitionIndexKey bit 
    ,ObjectID int 
);
GO

insert into #Table
(
    [SchemaName] 
    ,[TableName] 
    ,[FieldId]   
    ,[ColumnName]   
    ,Ordinal 
    ,[Data Type] 
    ,IsNullable 
    ,IsIdentity
    ,HasDefault 
    ,HasCheckConstraint
)
values
(
    N'dbo'
    ,N'test'
    ,-6
    ,N'RecordId'
    ,1
    ,N'int'
    ,0
    ,0
    ,0
    ,0
);

insert into #Table
(
    [SchemaName] 
    ,[TableName] 
    ,[FieldId]   
    ,[ColumnName]   
    ,Ordinal 
    ,[Data Type] 
    ,IsNullable 
    ,IsIdentity
    ,HasDefault 
    ,HasCheckConstraint
)
values
(
    N'dbo'
    ,N'test'
    ,-6
    ,N'NewField'
    ,2
    ,N'NewFieldType'
    ,0
    ,0
    ,0
    ,0
);
GO

declare @NewLine nchar(2) = nchar(13) + nchar(10);

with [Tables] AS
(
    select distinct
        [SchemaName],
        [TableName],
        N'[' + [SchemaName] + N'].[' + [TableName] + N']' AS [FullTableName]
    from
        #Table
)
select
    N'create table ' + [FullTableName] + @NewLine +
    N'(' + @NewLine +
    N'    ' + REPLACE(STUFF((select
                                 N',[' + [ColumnName] + N'] '
                                 + [Data Type] +
                                 + case when [IsNullable] = 0 then N' not null' else N' null' end
                                 + case when [IsIdentity] = 1 then N' identity' else N'' end
                                 + case when [HasDefault] = 1 then N' constraint [' + [DefaultName] + N'] default ' + [DefaultDefinition] else N'' end
                             from
                                 #Table
                             where
                                 [SchemaName] = T.[SchemaName] AND
                                 [TableName] = T.[TableName]
                             order by
                                 [Ordinal]
                             for xml path('')
                            ), 1, 1, N''
                      ), N',', @NewLine + N'    ,') + @NewLine +
    N'    ,[CreateDate] datetime not null' + @NewLine +
    N');' + @NewLine +
    @NewLine +
    N'exec sys.sp_addextendedproperty @name = N''EntityId''' + @NewLine +
    N'                                ,@value = N''' + cast(11 as nvarchar(max)) + N'''' + @NewLine +
    N'                                ,@level0type = N''Schema'', @level0name = N''' + [t].[SchemaName] + N'''' + @NewLine +
    N'                                ,@level1type = N''Table'', @level1name = N''' + [t].[TableName] + N''';' + @NewLine +
    @NewLine +
    N'exec sys.sp_addextendedproperty @name = N''MessageId''' + @NewLine +
    N'                                ,@value = N''' + cast(3456 as nvarchar(max)) + N'''' + @NewLine +
    N'                                ,@level0type = N''Schema'', @level0name = N''' + [t].[SchemaName] + N'''' + @NewLine +
    N'                                ,@level1type = N''Table'', @level1name = N''' + [t].[TableName] + N''';' + @NewLine
from
    [Tables] as T
order by
    T.[SchemaName],
    T.[TableName];