[英]SQL Server - return column types of query
Given a query, I want to return an information row that shows the types of each column returned. 给定一个查询,我想返回一个信息行,该信息行显示返回的每一列的类型。
I researched using: 我研究了使用:
SELECT system_type_name
FROM
sys.dm_exec_describe_first_result_set
('select * from table', NULL, 0) ;
That gives what I want for simple select statements: 这给出了我想要的简单选择语句:
But when using this for the select statements I want to run I get the following errors: 但是,当我将其用于select语句时,会出现以下错误:
What alternatives are there for grabbing columns for results set of a query? 有哪些替代方法可获取查询结果集的列?
I think you need below two Query's one of them 我认为您需要以下两个查询之一
for Datatype and Length 用于数据类型和长度
use yourdatabasename;
SELECT c.name AS 'Column Name',
t.name + '(' + cast(c.max_length as varchar(50)) + ')' As 'DataType',
case
WHEN c.is_nullable = 0 then 'null' else 'not null'
END AS 'Constraint'
FROM sys.columns c
JOIN sys.types t
ON c.user_type_id = t.user_type_id
WHERE c.object_id in ( Object_id('ordertable'), Object_id('Course'))
---here ordertable,Course is two table name ---这里的ordertable,当然是两个表名
SELECT
c.name 'Column Name',
t.Name 'Data type',
c.max_length 'Max Length',
c.precision ,
c.scale ,
c.is_nullable,
ISNULL(i.is_primary_key, 0) 'Primary Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
c.object_id = OBJECT_ID('YourTableName')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.