当前位置:   article > 正文

SQLServer 查询所有数据库名和表名及表结构等_查询数据库中所有表名

查询数据库中所有表名

1、查询所有数据库

SELECT name FROM sysdatabases

2、查询当前数据库中所有表名,不用指定数据库,选中某数据库直接执行SQL就好

  1. -- 'U':所有用户表名; 'S':所有系统表名;'V':所有视图表名
  2. SELECT name FROM sysobjects WHERE xtype='U' OR xtype='S' OR xtype='V'

3、获取指定表的主键字段

SELECT name AS 'PK' FROM SysColumns WHERE id=Object_Id('Table') AND colid=(SELECT TOP 1 keyno FROM sysindexkeys WHERE id=Object_Id('Table'))

4、查询指定表中的所有字段名

SELECT name FROM SysColumns WHERE id=Object_Id('Table_2') 

5、查询指定表中的所有字段名和字段类型

SELECT sc.name,st.name FROM SysColumns sc,systypes st WHERE sc.xtype=st.xtype AND sc.id in(SELECT id from sysobjects WHERE xtype='U' AND name='Table')

6、获取表部分数据结构

  1. SELECT c.name,
  2. c.user_type_id,
  3. c.max_length,
  4. c.is_nullable,
  5. remark = ex.value
  6. FROM sys.columns c
  7. LEFT OUTER JOIN sys.extended_properties ex ON ex.major_id = c.object_id AND ex.minor_id = c.column_id AND ex.name = 'MS_Description'
  8. WHERE
  9. OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
  10. AND OBJECT_NAME(c.object_id) = 'Table'
  11. ORDER
  12. BY OBJECT_NAME(c.object_id), c.column_id

7、SqlServer2000获取表结构详细信息

  1. SELECT TBL.name AS '表名',
  2. CONVERT(NVARCHAR(500),DSPTN.value) AS '表说明',
  3. COL.column_id AS '字段序号',
  4. COL.name AS '字段名',
  5. CASE WHEN COL.is_identity = 1 THEN 'YES' ELSE 'NO' END AS '标识',
  6. CASE WHEN PK.object_id IS NOT NULL AND PK.index_id = COL.column_id THEN 'YES' ELSE 'N0' END AS '主键',
  7. TY.name AS '类型',
  8. CAST(COL.max_length AS VARCHAR) AS '占用字节数',
  9. CAST(COL.precision AS VARCHAR) AS '长度',
  10. CAST(COL.scale AS VARCHAR) AS '小数位数',
  11. CASE WHEN COL.is_nullable = 1 THEN 'YES' ELSE 'NO' END AS '允许空',
  12. ISNULL(CONVERT(NVARCHAR(500), DFTCNST.definition), '') AS '默认值',
  13. CONVERT(NVARCHAR(500),SCOLMS.value) AS '字段说明'
  14. FROM sys.tables TBL
  15. INNER JOIN sys.columns COL ON TBL.object_id = COL.object_id
  16. LEFT JOIN sys.identity_columns IDCOL ON TBL.object_id = IDCOL.object_id AND COL.column_id = IDCOL.column_id
  17. LEFT JOIN sys.types TY ON COL.user_type_id = TY.user_type_id
  18. LEFT JOIN (
  19. SELECT
  20. name,
  21. object_id,
  22. index_id
  23. FROM sys.indexes
  24. WHERE is_primary_key = 1
  25. ) PK ON TBL.object_id = PK.object_id
  26. LEFT JOIN sys.default_constraints DFTCNST ON COL.default_object_id = DFTCNST.object_id
  27. LEFT JOIN sys.extended_properties SCOLMS ON COL.object_id = SCOLMS.major_id AND
  28. COL.column_id = SCOLMS.minor_id AND
  29. SCOLMS.name = 'MS_Description'
  30. LEFT JOIN sys.extended_properties DSPTN ON TBL.object_id = DSPTN.major_id AND DSPTN.minor_id = 0
  31. AND DSPTN.name = 'MS_Description'
  32. WHERE TBL.name = 'table'
  33. ORDER BY TBL.name, COL.column_id

8、SqlServer2000以上获取表结构详细信息

  1. SELECT TBL.name AS '表名',
  2. CONVERT(NVARCHAR(500),DSPTN.value) AS '表说明',
  3. COL.column_id AS '序号',
  4. COL.name AS '字段名',
  5. CASE WHEN COL.is_identity = 1 THEN 'YES' ELSE 'NO' END AS '标识',
  6. CASE WHEN PK.object_id IS NOT NULL AND PK.index_id = COL.column_id THEN 'YES' ELSE 'N0' END AS '主键',
  7. TY.name AS '类型',
  8. CAST(COL.max_length AS VARCHAR) AS '占用字节数',
  9. CAST(COL.precision AS VARCHAR) AS '长度',
  10. CAST(COL.scale AS VARCHAR) AS '小数位数',
  11. CASE WHEN COL.is_nullable = 1 THEN 'YES' ELSE 'NO' END AS '允许空',
  12. ISNULL(CONVERT(NVARCHAR(500), DFTCNST.definition), '') AS '默认值',
  13. CONVERT(NVARCHAR(500),SCOLMS.value) AS '字段说明'
  14. FROM sys.tables TBL
  15. INNER JOIN sys.columns COL ON TBL.object_id = COL.object_id
  16. LEFT JOIN sys.identity_columns IDCOL ON TBL.object_id = IDCOL.object_id AND COL.column_id = IDCOL.column_id
  17. LEFT JOIN sys.types TY ON COL.user_type_id = TY.user_type_id
  18. LEFT JOIN (
  19. SELECT
  20. name,
  21. object_id,
  22. index_id
  23. FROM sys.indexes
  24. WHERE is_primary_key = 1
  25. ) PK ON TBL.object_id = PK.object_id
  26. LEFT JOIN sys.default_constraints DFTCNST ON COL.default_object_id = DFTCNST.object_id
  27. LEFT JOIN sys.extended_properties SCOLMS ON COL.object_id = SCOLMS.major_id AND
  28. COL.column_id = SCOLMS.minor_id AND
  29. SCOLMS.name = 'MS_Description'
  30. LEFT JOIN sys.extended_properties DSPTN ON TBL.object_id = DSPTN.major_id AND DSPTN.minor_id = 0
  31. AND DSPTN.name = 'MS_Description'
  32. WHERE TBL.name = 'table'
  33. ORDER BY TBL.name, COL.column_id

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/703807
推荐阅读
相关标签
  

闽ICP备14008679号