执行具有多个结果集的存储过程 [英] Execute Stored Procedure with multiple result sets

查看:124
本文介绍了执行具有多个结果集的存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SSIS2016.我需要执行一个存储过程,该存储过程返回4个结果集.我只需要保留第一个结果集并将其写入表即可.我无法修改存储过程.我不在乎其他结果集中返回的任何数据.存储过程在SQL Server 2016数据库中.结果也将驻留在SQL Server 2016中.

I am using SSIS 2016. I need to execute a stored procedure that returns 4 result sets. I only need to keep the first result set and write this to a table. I can not modify the stored procedure. I do not care about any of the data returned in the other result sets. The stored procedure is in a SQL Server 2016 database. Results will also reside in SQL Server 2016.

我目前正在SSIS 2008中使用以下OLE DB源中的"SQL Command"数据访问模式运行此过程.我在For Each循环容器中将其传递给存储过程,以将一系列参数值传递给存储过程,因为我每天对不同的参数值执行多次.

I currently have this process running in SSIS 2008 using the "SQL Command" data access mode in an OLE DB Source like below. I have this in a For Each Loop Container to pass a series of param values to the stored procedure as I execute it multiple times for different param values on a daily basis.

SET FMTONLY OFF;

EXEC myProc
     @Param1 = ?,
     @Param2 =?,
     @Param3 = ?;

默认情况下,SSIS 2008仅返回第一个结果集,这对我有用,因为我只关心第一个结果集.

By default SSIS 2008 is only returning the first result set, which has worked for me as I only care about the first result set.

我正在使用本机OLEDB SQL Server客户端.据我了解,它改变了处理多个结果集的方式.我已经使用WITH RESULT SETS定义了第一个结果集,但是如果我执行SSIS将失败,则表明需要定义其他结果集.

I am using the Native OLEDB SQL Server client. From what I have read, it has changed the way it handles multiple result sets. I have used the WITH RESULT SETS to define the first result set but if I execute SSIS will fail indicating other result sets need to be defined.

简而言之,在SSIS 2016中复制在SSIS 2008中起作用的最佳方法是什么?

In short, what is the best approach to duplicate what works in SSIS 2008 in SSIS 2016?

推荐答案

解决方案概述

我对该问题进行了2个实验,第一个实验表明,在没有参数的存储过程的情况下,SQL Server 2016和SSIS 2016中没有任何更改,将返回第一个结果集,而其他结果集将被忽略.

Solution Overview

I made 2 Experiments on that issue, the first experiments showed that in case of stored procedures with no parameters, nothing changed in SQL Server 2016 and SSIS 2016, the first Result Set is returned and others are ignored.

第二个实验表明,使用参数时,这将引发异常,因此您必须使用WITH RESULT SETS选项定义元数据,然后删除此选项.

The second experiment showed that when using parameters, this will throw an exception, so you have to define metadata using WITH RESULT SETS option, then remove this option.

以下实验是使用SQL Server 2016和带有SSDT 2016的Visual Studio 2015进行的

  1. 首先,我创建了此存储过程

  1. First i created this stored procedure

CREATE PROCEDURE sp_Test


AS
BEGIN

SET NOCOUNT ON;

SELECT TOP 10 PersonType,NameStyle,Title 
  FROM [AdventureWorks2016CTP3].[Person].[Person]

SELECT  TOP 10 PersonType,Firstname,Lastname
  FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
GO

  • 然后我将数据流任务添加到SSIS包中
  • 添加了OLEDB源,记录集目标
  • 在OLEDB源中,我将数据访问模式选择为SQL command
  • 使用以下命令

  • Then i added a Data flow task to SSIS package
  • Added an OLEDB Source, Recordset destination
  • In OLEDB source i select the Data access mode to SQL command
  • an use the following commnad

    EXEC sp_Test
    

    1. 单击列"选项卡时,将显示第一个ResultSet结构

    1. 我们执行了成功运行的程序包

    我将存储过程更改为以下内容:

    I changed the stored procedures to the following:

    ALTER PROCEDURE [dbo].[sp_Test]
    
        @param1 varchar(10),
        @param2 varchar(10),
        @param3 varchar(10)
    AS
    BEGIN
    
        SET NOCOUNT ON;
    
    
        SELECT TOP 10 PersonType,NameStyle,Title ,@param2 as 'Param'
      FROM [AdventureWorks2016CTP3].[Person].[Person]
    
    
        SELECT  TOP 10 PersonType,Firstname,Lastname,@param3 as 'Param'
      FROM [AdventureWorks2016CTP3].[Person].[Person_json]
    END
    

    我在OLEDB源代码中使用了以下SQL命令:

    And i used the following SQL Command in the OLEDB Source:

    EXEC sp_Test ?,?,?
    
    WITH RESULT SETS (
    (
        PersonType NVarchar(10),
        NameStyle NVarchar(10),
        Title  NVarchar(10),
        Param Varchar(10)
    )
    )
    

    我正确地映射了参数.

    运行程序包时,它将引发以下异常.

    When running the package it throws the following exception.

    [OLE DB源 2 ]错误:SSIS错误代码DTS_E_OLEDBERROR.发生OLE DB错误.错误代码:0x80040E14. OLE DB记录可用.源:"Microsoft SQL Server本机客户端11.0"结果:0x80040E14说明:"EXECUTE语句失败,因为它的WITH RESULT SETS子句指定了1个结果集,并且该语句尝试发送的结果集大于此."

    [OLE DB Source 2] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E14. An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80040E14 Description: "EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), and the statement tried to send more result sets than this.".

    之后,我尝试删除With RESULT SETS选项,因此命令为:

    After that i tried to remove the With RESULT SETS option, so the command is :

    EXEC sp_Test ?,?,?
    

    我尝试再次执行该程序包,因此该程序包无任何错误地执行.

    I tried to execute the package again, so it is executed with no errors.

    尝试使用WITH RESULT SETs选项定义OLEDB Source metadata,在定义元数据之后,只需删除此选项并运行程序包,这样它将成功获取第一个结果集.

    Try to use the WITH RESULT SETs option to define the OLEDB Source metadata, after that the metadata is defined, just remove this option and run the package, so it will just take the first Result Set succesfully.

    这篇关于执行具有多个结果集的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆