tsql - Querying Column Definition for Data Length - SQL Server
问题描述
I am looking to generate a list of all the tables, columns, and datatypes for a database.
And this query will give me all information except for the length of the data types:
SELECT t.name AS TableName,
c.name AS ColumnName,
y.name AS DataType
FROM sys.tables AS t
JOIN sys.columns AS c ON t.object_id = c.object_id
JOIN sys.types AS y ON y.user_type_id = c.user_type_id
WHERE t.name = 'Tablename';
But for the columns with data types of varchar
or nvarchar
, I would like to also see the max limit for each.
For instance, if ColumnA is varchar(100)
- where can I find the 100 limit in the database? So far, I cannot find where that information is stored.
Thanks,
解决方案
sys.columns
有一个属性叫做max_length
:
SELECT
t.name AS TableName,
c.name AS ColumnName,
c.max_length,
y.name AS DataType
FROM
sys.tables AS t
JOIN
sys.columns AS c ON t.object_id = c.object_id
JOIN
sys.types AS y ON y.user_type_id = c.user_type_id
WHERE
t.name = 'Tablename';
有关详细信息,请参阅官方 MS 文档sys.columns
推荐阅读
- c++ - 使用 gtest 测试 c 文件时使用 extern c 时未解析的外部符号 (LNK2019)
- java - 如何使用 parquet avro Schema 存储以数字开头的 ColumnName?
- swift - 如何在 MacOS Swift 中隐藏 QLPreviewPanel 中的顶部项目
- c# - 有没有办法为同一类的“不同级别”(例如集合)共享私有字段?
- json - 如何使用 kotlin 程序替换 apk 中的 json 文件,以便在数据有任何变化时只能更改“json 文件”而不是更新 apk?
- java - 启动层 java.lang.module.FindException 初始化时出错:未找到模块 org.slf4j,org.apache.poi.ooxml 需要
- javascript - 导航栏的 React JS 输出未显示
- typo3 - TYPO3 10.4:“arguments”注册为“array”类型,但属于“string”类型!- 刷新系统缓存后仅出现一次
- apache-spark - 直接从 DWH 通过 PySpark 处理数据是一种好方法吗?
- ruby - 如何评估另一个 ruby 文件的代码?