如何从存储过程resutls中检索指定的列而不是全部?

时间:2018-03-03 09:13:22

标签: sql-server stored-procedures sql-server-2012

sp_describe_first_result_set  @tsql=N'%s'

返回了很多信息。但我只想要namesystem_type_name

是否可以在返回的结果中指定列?

2 个答案:

答案 0 :(得分:1)

您可以先将所有列转储到临时表中,然后只选择必要的列。

create table #1 (name varchar(50), system_type_name varchar(50), col3 int, col4 int)
 insert into #1 (name, system_type_name, col3, col4)
        exec dbo.sp_describe_first_result_set
      select name, system_type_name from #1
  drop table #1

答案 1 :(得分:1)

要添加@Steef的答案,下面是与documented结果集匹配的表定义:

DECLARE @results TABLE(
      is_hidden bit NOT NULL
    , column_ordinal int NOT NULL   
    , name sysname NULL
    , is_nullable bit NOT NULL
    , system_type_id int NOT NULL
    , system_type_name sysname NULL
    , max_length smallint NOT NULL
    , precision tinyint NOT NULL
    , scale tinyint NOT 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 NOT NULL
    , is_case_sensitive bit NOT NULL
    , is_fixed_length_clr_type bit NOT 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_is_descending smallint NULL
    , order_by_list_length smallint NULL
    , tds_type_id int NOT NULL
    , tds_length int NOT NULL
    , tds_collation_id int NULL
    , tds_collation_sort_id tinyint NULL
);