SQL语句来获取一个表的所有列的信息,如,列名、类型、长度等(转)

本代码适用于: SQLSERVER2000/2005
SQL语句如下:

select c.name, t.name as type, c.length
  ,(
case t.name
    
when 'nvarchar' then c.length/2
    
when 'nchar' then c.length/2
    
else c.length
  
end)
  
as reallength
from syscolumns c join systypes t
on c.xtype=t.xtype
where t.name <> 'sysname' and c.id=object_id('Table1')

-- 加了这句代码,可以使列按设计时的顺序输出

order by colorder ASC


为了方便使用,可将其封装成一个函数代码如下:

--
--
 返回一个表的列信息
--
 用法:select * from tbl_columns('Table1')
--
 zyl 2007.11.6
--
create   function  tbl_columns( @tablename   nvarchar ( 256 ))
returns   @tmptb   table (
  name 
nvarchar ( 256 ),
  type 
varchar ( 256 ),
  length 
int ,
  reallength 
int
)
begin
insert   into   @tmptb
select  c.name, t.name  as  type, c.length
  ,(
case  t.name
    
when   ' nvarchar '   then  c.length / 2
    
when   ' nchar '   then  c.length / 2
    
else  c.length
  
end )
  
as  reallength
from  syscolumns c  join  systypes t
on  c.xtype = t.xtype
where  t.name  <>   ' sysname '   and  c.id = object_id ( @tablename )
return  
end

转载于:https://www.cnblogs.com/williamwindy/archive/2009/01/19/1378199.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值