car*_*osm 4 sql-server sql-server-2014
假设我在 SQL 2014 中有一个查询:
SELECT EmployeeName, EmployeeAddress, EmployeeAge FROM dbo.Employee
Run Code Online (Sandbox Code Playgroud)
我想动态地进行查询、循环并获取EmployeeName、EmployeeAddress和 等列的名称EmployeeAge。
我需要这个,因为我可以有另一个与此不同的查询,我还需要获取列名。
小智 5
存储sp_describe_first_result_set过程将为您提供列名以及任何查询的更多信息。您只需将相关查询传递给参数即可@tsql。请参阅下面存储过程的使用示例:
DECLARE @queryDescription TABLE
(
s_hidden bit NULL
,column_ordinal int NULL
,name sysname NULL
,is_nullable bit NULL
,system_type_id int NULL
,system_type_name nvarchar(256) NULL
,max_length smallint NULL
,precision tinyint NULL
,scale tinyint NULL
,collation_name sysname NULL
,user_type_id int NULL
,user_type_database sysname NULL
,user_type_schema sysname NULL
,user_type_name sysname NULL
,assembly_qualified_type_name nvarchar(4000) NULL
,xml_collection_id int NULL
,xml_collection_database sysname NULL
,xml_collection_schema sysname NULL
,xml_collection_name sysname NULL
,is_xml_document bit NULL
,is_case_sensitive bit NULL
,is_fixed_length_clr_type bit NULL
,source_server sysname NULL
,source_database sysname NULL
,source_schema sysname NULL
,source_table sysname NULL
,source_column sysname NULL
,is_identity_column bit NULL
,is_part_of_unique_key bit NULL
,is_updateable bit NULL
,is_computed_column bit NULL
,is_sparse_column_set bit NULL
,ordinal_in_order_by_list smallint NULL
,order_by_list_length smallint NULL
,order_by_is_descending smallint NULL
,tds_type_id int NULL
,tds_length int NULL
,tds_collation_id int NULL
,tds_collation_sort_id tinyint NULL
)
DECLARE @query NVARCHAR(MAX) = 'SELECT EmployeeName, EmployeeAddress, EmployeeAge FROM dbo.Employee'
INSERT INTO @queryDescription
EXEC sp_describe_first_result_set @tsql = @query
SELECT Name AS ColumnName
,system_type_name AS DataTypeName
,column_ordinal AS Ordinal
FROM @queryDescription
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6880 次 |
| 最近记录: |