1.生成下面的procedure后运行
exec up_getTableStruct '你的表名'
就可以抓取到表的结构create procedure up_getTableStruct 
@v_tableName varchar(256) 
as 
declare @i_objectId int, -- 對象id 
@i_indId smallint, -- 索引id 
@v_pkInfo varchar(100), -- 主鍵信息 
@v_clusteredInfo varchar(20), -- clustered信息 
@v_pkCol varchar(100), -- 主鍵字段 
@v_key varchar(50), 
@i_i smallint 
set @i_objectId = object_id(@v_tableName) 
if @i_objectId is null -- 判斷對象是否存在 
begin 
print 'The object not exists' 
return 
end 
if OBJECTPROPERTY(@i_objectId,'IsTable') <> 1 -- 判斷對象是否是table 
begin 
print 'The object is not table' 
return 
end create table #temp1 

i_id int identity, 
v_desc varchar(200) 
) insert into #temp1(v_desc) 
values('create table '+@v_tableName+'(') -- insert into #temp1(v_desc) -- 將表的字段信息存入臨時表 
select a.name+space(4)+b.name+ 
case when b.xtype in (167,175,231,239) then '('+cast(a.length as varchar)+')' 
when b.xtype in (106,108) then '('+cast(a.xprec as varchar)+','+cast(a.xscale as varchar)+')' 
else '' end+space(4)+ 
case when (a.colstat & 1 = 1) then 'identity('+cast(ident_seed(@v_tableName) as varchar)+',' +
cast(ident_incr(@v_tableName) as varchar)+')' else '' end +space(4)+ 
case a.isnullable when 0 then 'not null' else 'null' end+'|' 
from syscolumns a,systypes b 
where a.id = @i_objectId and a.xtype = b.xusertype 
order by a.colid if exists(select 1 from sysobjects where parent_obj = @i_objectId and xtype = 'PK') -- 如果存在主鍵 
begin 
select @v_pkInfo = b.name,@i_indId = indid, -- 得到主鍵名,id及是否clustered信息 
@v_clusteredInfo = (case when (a.status & 16)=16 then 'clustered' else 'nonclustered' end ) 
from sysindexes a,sysobjects b 
where a.id = b.parent_obj and a.name = b.name and b.xtype = 'PK' and b.parent_obj = @i_objectId 

select @v_pkCol = index_col(@v_tableName, @i_indId, 1), @i_i = 2 -- 得到主鍵的第1個字段名 
select @v_key = index_col(@v_tableName, @i_indId, @i_i) -- 得到主鍵的第2個字段名 
while (@v_key is not null) 
begin 
select @v_pkCol = @v_pkCol + ',' + @v_key, @i_i = @i_i + 1 
select @v_key = index_col(@v_tableName, @i_indId, @i_i) -- 得到主鍵的第@i_i個字段名 
end -- 組合成主鍵信息 
set @v_pkInfo = 'constraint '+@v_pkInfo+' primary key '+@v_clusteredInfo+'('+@v_pkCol+')' 
insert into #temp1(v_desc) values(@v_pkInfo) -- 將主鍵信息插入臨時表 
end 
else 
begin 
select @i_i = count(1) from #temp1 
-- 如果沒有主鍵,那麼將最後一筆紀錄的'|'去掉 
update #temp1 set v_desc = replace(v_desc,'|','') where i_id = @i_i 
end insert into #temp1(v_desc) values(')') -- 
update #temp1 set v_desc = replace(v_desc,'|',',') select v_desc from #temp1 order by i_id drop table #temp1
2.用alter table 你的表名 ……
就可以更改表的结构,至于alter table语句的用法请查看online book,很简单的。

解决方案 »

  1.   

    下面的语句也许可以帮你一下select table_no=id,Field_en=name,Field_cn='',
    field_type=(select top 1 name from systypes where systypes.xtype=a.xtype),
    is_null=case isnullable when 1 then 'True' else 'False' end,
    is_key=case (select top 1 indid from sysindexkeys where colid = a.colid and id=a.id) when 1 then 'True' else 'False' end,
    default_value=(select top 1 text from syscomments where id=cdefault),
    var_a=length,var_b=xprec,var_c=xscale from syscolumns a
    where id = object_id( '表名' ) order by idfield_type --字段类型
    is_null -- 是否空值
    is_key --主键
    default_value --缺省值
    其它为长度值 如 varchar( 400 ) 中的 400还可以取外键,唯一性,check等
      

  2.   

    所有字段及其类型,允许最大长度(字符),列默认值,是否为空。
    select COLUMN_NAME,DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,  COLUMN_DEFAULT, IS_NULLABLE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = tbName 有没有键?
    select OBJECTPROPERTY(OBJECT_ID(tbName) , 'TableHasPrimaryKey')
    键是什么?
    select COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME = tbName
      

  3.   

    感觉SQL版的人都很厉害 :)
    to: IronPromises(铁诺) 
    你的这个存储过程比系统的那个还复杂 我先试一下吧 我想应该可以达到目的 不管怎么样你的100分是有了 
    当然我希望有简单一点的方法,能够直接取得数据结构,还有100分,大家帮个忙,我明天过来结贴
      

  4.   

    to: imzj(云网) 
    用你的方法试了一下,不知道为什么显示不出记录??
      

  5.   

    直接取得数据结构:select * from yourtable where 1=2
      

  6.   

    下面的是N_Chow(一剑飘香)兄曾写过的语句:
    请注意将语句中的DB服务器的ip地址,数据库名,表名换成你自己的:DECLARE @object int
    DECLARE @hr int
    DECLARE @src varchar(255), @desc varchar(255)
    Declare @objDatabase int,@objTable int,@Scrip varchar(8000) 
     
    EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @object OUT
    IF @hr <> 0
    BEGIN
       EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 
       SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
        RETURN
    ENDEXEC @hr = sp_OAMethod @object, 'Connect', NULL, '10.60.80.21','sa'
    IF @hr <> 0
    BEGIN
        EXEC sp_OAGetErrorInfo @object
        RETURN
    ENDEXEC @hr = sp_OAMethod @object, 'Databases', @objDatabase OUTPUT,'Northwind'
    IF @hr <> 0
    BEGIN
       EXEC sp_OAGetErrorInfo @object
        RETURN
    END EXEC @hr = sp_OAMethod @objDatabase, 'Tables', @objTable OUTPUT,'Orders'
    IF @hr <> 0
    BEGIN
       EXEC sp_OAGetErrorInfo @objDatabase
        RETURN
    END EXEC @hr = sp_OAMethod @objTable, 'Script', @Scrip  OUTPUT ,4
    IF @hr <> 0
    BEGIN
       EXEC sp_OAGetErrorInfo @object
        RETURN
    END 
    Print @Scrip
      

  7.   

    铁板竹:试验了一下你的存储过程,发现了一个错误,希望能改进一下:
    字段类型 nvarchar(10) 变成了nvarchar(20),nchar(10) 变成了nchar(20)
      

  8.   

    呵呵,谢谢海兄提醒,这个错误确实存在,更改过的sp如下:create procedure up_getTableStruct 
    @v_tableName varchar(256) 
    as 
    declare @i_objectId int, -- 對象id 
    @i_indId smallint, -- 索引id 
    @v_pkInfo varchar(100), -- 主鍵信息 
    @v_clusteredInfo varchar(20), -- clustered信息 
    @v_pkCol varchar(100), -- 主鍵字段 
    @v_key varchar(50), 
    @i_i smallint 
    set @i_objectId = object_id(@v_tableName) 
    if @i_objectId is null -- 判斷對象是否存在 
    begin 
    print 'The object not exists' 
    return 
    end 
    if OBJECTPROPERTY(@i_objectId,'IsTable') <> 1 -- 判斷對象是否是table 
    begin 
    print 'The object is not table' 
    return 
    end 
    set nocount on
    create table #temp1 

    i_id int identity, 
    v_desc varchar(200) 
    ) insert into #temp1(v_desc) 
    values('create table '+@v_tableName+'(') -- insert into #temp1(v_desc) -- 將表的字段信息存入臨時表 
    select a.name+space(4)+b.name+ 
    case when b.xtype in (167,175) then '('+cast(a.length as varchar)+')' 
    when b.xtype in (231,239) then '('+cast(a.length/2 as varchar)+')' 
    when b.xtype in (106,108) then '('+cast(a.xprec as varchar)+','+cast(a.xscale as varchar)+')' 
    else '' end+space(4)+ 
    case when (a.colstat & 1 = 1) then 'identity('+cast(ident_seed(@v_tableName) as varchar)+',' +
    cast(ident_incr(@v_tableName) as varchar)+')' else '' end +space(4)+ 
    case a.isnullable when 0 then 'not null' else 'null' end+'|' 
    from syscolumns a,systypes b 
    where a.id = @i_objectId and a.xtype = b.xusertype 
    order by a.colid if exists(select 1 from sysobjects where parent_obj = @i_objectId and xtype = 'PK') -- 如果存在主鍵 
    begin 
    select @v_pkInfo = b.name,@i_indId = indid, -- 得到主鍵名,id及是否clustered信息 
    @v_clusteredInfo = (case when (a.status & 16)=16 then 'clustered' else 'nonclustered' end ) 
    from sysindexes a,sysobjects b 
    where a.id = b.parent_obj and a.name = b.name and b.xtype = 'PK' and b.parent_obj = @i_objectId 

    select @v_pkCol = index_col(@v_tableName, @i_indId, 1), @i_i = 2 -- 得到主鍵的第1個字段名 
    select @v_key = index_col(@v_tableName, @i_indId, @i_i) -- 得到主鍵的第2個字段名 
    while (@v_key is not null) 
    begin 
    select @v_pkCol = @v_pkCol + ',' + @v_key, @i_i = @i_i + 1 
    select @v_key = index_col(@v_tableName, @i_indId, @i_i) -- 得到主鍵的第@i_i個字段名 
    end -- 組合成主鍵信息 
    set @v_pkInfo = 'constraint '+@v_pkInfo+' primary key '+@v_clusteredInfo+'('+@v_pkCol+')' 
    insert into #temp1(v_desc) values(@v_pkInfo) -- 將主鍵信息插入臨時表 
    end 
    else 
    begin 
    select @i_i = count(1) from #temp1 
    -- 如果沒有主鍵,那麼將最後一筆紀錄的'|'去掉 
    update #temp1 set v_desc = replace(v_desc,'|','') where i_id = @i_i 
    end insert into #temp1(v_desc) values(')') -- 
    update #temp1 set v_desc = replace(v_desc,'|',',') select v_desc from #temp1 order by i_id drop table #temp1
      

  9.   

    cadinfo(无语清风) :
    你太客气了。谢谢。