如何更改此动态查询

时间:2019-05-07 14:32:07

标签: sql sql-server dynamic-sql

我去了一个动态查询,从#Table中的数据创建新表。 我想将该查询更改为在创建语句中具有CreateDate字段。
但是我不知道该怎么做。

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'
    );

    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 
    );

    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';

但是我想在创建表语句中有一个CreateDate字段,就像这样:

    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';

但我不知道如何更改查询以获取此结果。

1 个答案:

答案 0 :(得分:1)

您只需要将文字文本添加到生成的字符串中即可。将查询更新为:

{{1}}