SQL Server查询表结构语句

--1:获取当前数据库中的所有用户表   www.2cto.com  
select Name from sysobjects where xtype='u' and status>=0 
--2:获取某一个表的所有字段 
select name from syscolumns where id=object_id('表名') 
--3:查看与某一个表相关的视图、存储过程、函数 
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%' 
 
--4:查看当前数据库中所有存储过程 
select name as 存储过程名称 from sysobjects where xtype='P' 
--5:查询用户创建的所有数据库 
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa') 
或者 
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01 
 
--6:查询某一个表的字段和数据类型 
select column_name,data_type from information_schema.columns 
where table_name = '表名'
 
--7:获取一个表结构的字段,类型,描述,最大长度,
复制代码
select
  cast(t1.tables_name as nvarchar) tables_name,
  cast(t1.columns_name as nvarchar) columns_name,
  cast(t1.columns_type as nvarchar) columns_type,
  cast(t1.max_length as int) max_length,
  cast(t1.is_nullable as nvarchar) is_nullable,
  cast(sys.extended_properties.value as nvarchar) description,
  cast(t1.column_id as int) column_id
from
 (
  select
   sys.tables.name tables_name,
   sys.columns.name columns_name,
   sys.types.name columns_type,
   sys.columns.max_length max_length,
   sys.columns.is_nullable is_nullable,
   sys.columns.object_id object_id,
   sys.columns.column_id column_id
  from
   sys.columns,
   sys.tables,
   sys.types
  where
   sys.columns.object_id = sys.tables.object_id
  and sys.columns.system_type_id = sys.types.system_type_id
  and sys.columns.user_type_id = sys.types.user_type_id
  and sys.tables.name = '#tablename#表名'
 ) t1
left join sys.extended_properties on sys.extended_properties.major_id = t1.object_id
and sys.extended_properties.minor_id = t1.column_id
order by
 t1.column_id
复制代码

 

posted on   陈惟鲜的博客  阅读(673)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示