In SQL Server, how do I generate a CREATE TABLE statement for a given table?

前端 未结 16 2404
离开以前
离开以前 2020-11-22 11:34

I\'ve spent a good amount of time coming up with solution to this problem, so in the spirit of this post, I\'m posting it here, since I think it might be useful to others. <

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 11:55

    I include definitions for computed columns

        select 'CREATE TABLE [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name  + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' END, name
    from    sysobjects so
    cross apply
        (SELECT
    
    case when comps.definition is not null then '  ['+column_name+'] AS ' + comps.definition 
    else
            '  ['+column_name+'] ' + data_type + 
            case
            when data_type like '%text' or data_type in ('image', 'sql_variant' ,'xml')
                then ''
            when data_type in ('float')
                then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ')'
            when data_type in ('datetime2', 'datetimeoffset', 'time')
                then '(' + cast(coalesce(datetime_precision, 7) as varchar(11)) + ')'
            when data_type in ('decimal', 'numeric')
                then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ',' + cast(coalesce(numeric_scale, 0) as varchar(11)) + ')'
            when (data_type like '%binary' or data_type like '%char') and character_maximum_length = -1
                then '(max)'
            when character_maximum_length is not null
                then '(' + cast(character_maximum_length as varchar(11)) + ')'
            else ''
            end + ' ' +
            case when exists ( 
            select id from syscolumns
            where object_name(id)=so.name
            and name=column_name
            and columnproperty(id,name,'IsIdentity') = 1 
            ) then
            'IDENTITY(' + 
            cast(ident_seed(so.name) as varchar) + ',' + 
            cast(ident_incr(so.name) as varchar) + ')'
            else ''
            end + ' ' +
             (case when information_schema.columns.IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + 
              case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE '' END 
    end + ', ' 
    
         from information_schema.columns 
         left join sys.computed_columns comps 
         on OBJECT_ID(information_schema.columns.TABLE_NAME)=comps.object_id and information_schema.columns.COLUMN_NAME=comps.name
    
         where table_name = so.name
         order by ordinal_position
        FOR XML PATH('')) o (list)
    left join
        information_schema.table_constraints tc
    on  tc.Table_name       = so.Name
    AND tc.Constraint_Type  = 'PRIMARY KEY'
    cross apply
        (select '[' + Column_Name + '], '
         FROM   information_schema.key_column_usage kcu
         WHERE  kcu.Constraint_Name = tc.Constraint_Name
         ORDER BY
            ORDINAL_POSITION
         FOR XML PATH('')) j (list)
    where   xtype = 'U'
    AND name    NOT IN ('dtproperties')
    

提交回复
热议问题