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

本文介绍了一个SQL函数,用于查询指定表的所有列信息,包括列名、类型、长度及实际长度等。此函数通过连接系统表实现,并可根据具体表名调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

--
--
 返回一个表的列信息
--
 用法: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/yuhanzhong/archive/2013/01/11/2856111.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值