Register NOW
select'create table ['+ so.name +'] ('+ o.list +')'+CASEWHEN tc.Constraint_Name ISNULLTHEN''ELSE'ALTER TABLE '+ so.Name +' ADD CONSTRAINT '+ tc.Constraint_Name  +' PRIMARY KEY '+' ('+LEFT(j.List, Len(j.List)-1)+')'ENDfrom    sysobjects so
crossapply(SELECT'  ['+column_name+'] '+ 
        data_type +case data_type
            when'sql_variant'then''when'text'then''when'ntext'then''when'xml'then''when'decimal'then'('+ cast(numeric_precision as varchar)+', '+ cast(numeric_scale as varchar)+')'elsecoalesce('('+casewhen character_maximum_length =-1then'MAX'else cast(character_maximum_length as varchar)end+')','')end+' '+casewhenexists(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+' '+(casewhen IS_NULLABLE ='No'then'NOT 'else''end)+'NULL '+casewhen information_schema.columns.COLUMN_DEFAULT ISNOTNULLTHEN'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE''END+', 'from information_schema.columns where table_name = so.name
     orderby ordinal_position
    FORXML PATH('')) o (list)leftjoin
    information_schema.table_constraints tc
on  tc.Table_name       = so.Name
AND tc.Constraint_Type  ='PRIMARY KEY'crossapply(select'['+ Column_Name +'], 'FROM   information_schema.key_column_usage kcu
     WHERE  kcu.Constraint_Name = tc.Constraint_Name
     ORDERBY
        ORDINAL_POSITION
     FORXML PATH('')) j (list)where   xtype ='U'AND name    NOTIN('dtproperties')
Filter by filter