select rtrim(b.name) as colname
,case when h.id is not null then 'PK' else '' end as primarykey
,type_name(b.xusertype) + case when b.colstat & 1 = 1 then '[ID(' + convert(varchar,ident_seed(a.name)) + ',' + convert(varchar,ident_incr(a.name)) + ')]' else '' end as type
,b.length
,case b.isnullable when 0 then 'N' else 'Y' end as [isnull]
,isnull(e.text,'') as [default]
,isnull(c.value,'') as descript 
from sysobjects a,syscolumns b 
left outer join sysproperties c on b.id = c.id and b.colid = c.smallid 
left outer join syscomments e on b.cdefault = e.id
left outer join (select g.id,g.colid from sysindexes f,sysindexkeys g where f.id = g.id and f.indid = g.indid and f.indid > 0 and f.indid < 255 and (f.status & 2048)<>0) h on b.id = h.id and b.colid = h.colid
where a.id = b.id
and a.id = object_id('titles') --tablename改成你要导出的表的名称
order by b.colid

解决方案 »

  1.   

    select d.name,a.name ,b.name ,a.length, a.isnullable from syscolumns a, systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U'
      

  2.   

    SELECT 
    (case when a.colorder=1 then d.name else '' end) N'表名',
    a.colorder N'字段序号',
    a.name N'字段名',
    (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'标识',
    (case when (SELECT count(*)
    FROM sysobjects
    WHERE (name in
               (SELECT name
             FROM sysindexes
              WHERE (id = a.id) AND (indid in
                        (SELECT indid
                      FROM sysindexkeys
                       WHERE (id = a.id) AND (colid in
                                 (SELECT colid
                                FROM syscolumns
                               WHERE (id = a.id) AND (name = a.name))))))) AND
           (xtype = 'PK'))>0 then '√' else '' end) N'主键',
    b.name N'类型',
    a.length N'占用字节数',
    COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
    isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
    (case when a.isnullable=1 then '√'else '' end) N'允许空',
    isnull(e.text,'') N'默认值',
    isnull(g.[value],'') AS N'字段说明'
    --into ##txFROM  syscolumns  a left join systypes b 
    on  a.xtype=b.xusertype
    inner join sysobjects d 
    on a.id=d.id  and  d.xtype='U' and  d.name<>'dtproperties'
    left join syscomments e
    on a.cdefault=e.id
    left join sysproperties g
    on a.id=g.id AND a.colid = g.smallid  
    order by object_name(a.id),a.colorder
      

  3.   

    select d.name,a.name ,b.name ,a.length, a.isnullable from syscolumns a, systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and d.name in ('表1','表2')
      

  4.   

    SELECT 
    (case when a.colorder=1 then d.name else '' end) N'表名',
    a.colorder N'字段序号',
    a.name N'字段名',
    (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'标识',
    (case when (SELECT count(*)
    FROM sysobjects
    WHERE (name in
               (SELECT name
             FROM sysindexes
              WHERE (id = a.id) AND (indid in
                        (SELECT indid
                      FROM sysindexkeys
                       WHERE (id = a.id) AND (colid in
                                 (SELECT colid
                                FROM syscolumns
                               WHERE (id = a.id) AND (name = a.name))))))) AND
           (xtype = 'PK'))>0 then '√' else '' end) N'主键',
    b.name N'类型',
    a.length N'占用字节数',
    COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
    isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
    (case when a.isnullable=1 then '√'else '' end) N'允许空',
    isnull(e.text,'') N'默认值',
    isnull(g.[value],'') AS N'字段说明'
    --into ##txFROM  syscolumns  a left join systypes b 
    on  a.xtype=b.xusertype
    inner join sysobjects d 
    on a.id=d.id  and  d.xtype='U' and  d.name<>'dtproperties'
    left join syscomments e
    on a.cdefault=e.id
    left join sysproperties g
    on a.id=g.id AND a.colid = g.smallid  
    order by object_name(a.id),a.colorderdeclare @db varchar(30),@sql varchar(3000)
    set @db=db_name()
    set @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:\'+@db+'_exp.xls -c -Usa -P '''
    print @sql
    exec(@sql)select * from ##tx-------------------------------------------------------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 create procedure 名
    @aa varchar(10)
    as
    begin
      select a.id 编号,a.name 名称,b.id,b.name from table1 a join table2 b on a.id=b.id where 
    endif 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------------------------------------SELECT *
    FROM sysobjects
    WHERE id=object_id('ExpressPathCredit') and name='EPC_Id'
    (case (SELECT count(*)
    FROM sysobjects
    WHERE (name =
              (SELECT name
             FROM sysindexes
             WHERE (id = OBJECT_ID('TrsNews')) AND (indid =
                       (SELECT indid
                      FROM sysindexkeys
                      WHERE (id = OBJECT_ID('TrsNews')) AND (colid =
                                (SELECT colid
                               FROM syscolumns
                               WHERE (id = OBJECT_ID('TrsNews')) AND (name = 'TN_id'))))))) AND
           (xtype = 'PK'))>0 then 'PK' end)
    WHERE  a.xtype=b.xusertype  and  a.id=d.id  and  d.xtype='U' and a.cdefault=e.id
    SELECT *
    FROM syscommentsSELECT *
    FROM syscomments
    WHERE (id = '932198371')