5

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

我目前使用 OLE DB 源中的“SQL 命令”数据访问模式在 SSIS 2008 中运行此过程,如下所示。我在 For Each 循环容器中有这个,用于将一系列参数值传递给存储过程,因为我每天为不同的参数值多次执行它。

SET FMTONLY OFF;

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

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

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

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

4

2 回答 2

3

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.

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.


Detailed Solution

Experiment 1

The following experiment are made using SQL Server 2016 and Visual Studio 2015 with SSDT 2016

  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
    
  2. Then i added a Data flow task to SSIS package
  3. Added an OLEDB Source, Recordset destination
  4. In OLEDB source i select the Data access mode to SQL command
  5. an use the following commnad

    EXEC sp_Test
    

enter image description here

  1. When clicking on Columns Tab it shows the first ResultSet structure

enter image description here

  1. And we i executed the package it runs succesfully

enter image description here

Experiment 2

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

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)
)
)

And i mapped the parameters correctly.

enter image description here

enter image description here

When running the package it throws the following exception.

[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.".

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.

Conclusion

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.

于 2018-01-03T19:46:48.463 回答
0

我知道这并不完全符合您的要求,但也许您可以创建另一个只为您返回第一个结果集的存储过程?(无需触及第一个存储过程。)或者为您将数据插入表中,然后您只需读取数据?

于 2018-01-03T18:45:26.887 回答