2014-01-07 106 views

回答

1

右键单击该表中SSMS并选择脚本表>作为创建

4
  1. 右键单击表
  2. 脚本表为
  3. 创造
  4. 新的查询编辑器窗口

enter image description here

编辑:该死的你必须快速在这里为简单的代表!哈哈。

+0

+1为您编辑::) – Shiva

+1

感谢湿婆!在成为水蛭几年之后才刚刚开始。 –

1

如果你想从一些脚本做那么这里是

declare @table varchar(100) 
set @table = 'client_table' -- set table name here 
declare @sql table(s varchar(1000), id int identity) 

-- create statement 
insert into @sql(s) values ('create table [' + @table + '] (') 

-- column list 
insert into @sql(s) 
select 
    ' ['+column_name+'] ' + 
    data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' + 
    case when exists ( 
     select id from syscolumns 
     where object_name(id)[email protected] 
     and name=column_name 
     and columnproperty(id,name,'IsIdentity') = 1 
    ) then 
     'IDENTITY(' + 
     cast(ident_seed(@table) as varchar) + ',' + 
     cast(ident_incr(@table) as varchar) + ')' 
    else '' 
    end + ' ' + 
    (case when IS_NULLABLE = 'No' then 'NOT ' else '' end) + 'NULL ' + 
    coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ',' 

from information_schema.columns where table_name = @table 
order by ordinal_position 

-- primary key 
declare @pkname varchar(100) 
select @pkname = constraint_name from information_schema.table_constraints 
where table_name = @table and constraint_type='PRIMARY KEY' 

if (@pkname is not null) begin 
    insert into @sql(s) values(' PRIMARY KEY (') 
    insert into @sql(s) 
     select ' ['+COLUMN_NAME+'],' from information_schema.key_column_usage 
     where constraint_name = @pkname 
     order by ordinal_position 
    -- remove trailing comma 
    update @sql set s=left(s,len(s)-1) where [email protected]@identity 
    insert into @sql(s) values (' )') 
end 
else begin 
    -- remove trailing comma 
    update @sql set s=left(s,len(s)-1) where [email protected]@identity 
end 

-- closing bracket 
insert into @sql(s) values(')') 

-- result! 
select s from @sql order by id 

这会给你的输出像

create table [client_table] (
    [colA] varchar(250) NOT NULL DEFAULT (''), 
    [colB] int NOT NULL DEFAULT(0) 
)