How to EXEC a stored procedure from SSIS to get its output to text file

2

I wrote a stored procedure that reads records into a temp table and then creates a pivoted output from the data in the temp table.

Running the stored procedure from SSMS works just fine. What I am facing problems is now that I am trying to create the SSIS package that will execute the sproc, and write its output to a tab delimited text file.

I am using Visual Studio 2015. My first problem is that when I try to configure the OLE DB Source within the Data Flow Task, when adding the SQL Command inside the SQL Command Text box: EXEC ShopperSkuHistory and click OK, I get this error:

enter image description here

I have been looking for information about this error, but I have not found anything that helps me understand why this is happening, and how I can fix it.

I hope through this post I can learn how to fix this error.

Thank you much in advance.

Here is my sproc:

UPDATED CODE

ALTER PROCEDURE [dbo].[ShopperSkuHistory]

AS
BEGIN

        IF OBJECT_ID('tempdb..[#ShopperSku_History_Load]') IS NOT NULL
                BEGIN
                        DROP TABLE [#ShopperSku_History_Load];
                END;        

        -- Create main table
        CREATE TABLE [#ShopperSku_History_Load]
        (
            [ID]                                    INT IDENTITY(1, 1) NOT NULL
        ,   [shopper_id]    CHAR(32) NOT NULL
        ,   [sku]                               VARCHAR(100) NOT NULL                       
        , time_added  DATETIME      
        )       

        SET NOCOUNT ON; 

        -- Populate the table 
        INSERT INTO [#ShopperSku_History_Load] ([shopper_id], [sku], [time_added])      
        SELECT DISTINCT [cr].[shopper_id], LEFT([cri].[sku], 9) [sku], GETDATE() [time_added]
        FROM [dbo].[receipt_item] [cri]
        INNER JOIN [dbo].[receipt] [cr]
                ON [cri].[order_id] = [cr].[order_id]
        WHERE[cri].[list_price] > 0
                AND [cri].[IsInitialPurchase] = 1
                AND LEFT([cri].[sku], 3) = 'MN0'
                AND ([cr].[date_entered] > DATEADD(YEAR, -2, GETDATE()))
                AND EXISTS (SELECT 1 FROM [product] [cp] WHERE [cp].[pf_id] = [cri].[sku] AND [cp].[for_sale] = 1)
                AND NOT EXISTS (SELECT 1 FROM [dbo].[shopper] [cs] WHERE [cs].[IsTesting] = 1 AND [cs].[shopper_bounce] = [cr].[shopper_id])                
        ORDER BY [shopper_id];  

        CREATE TABLE [#HistoryOutput] 
        (
        [shopper_id] VARCHAR(32)
        , skus TEXT
        )

        INSERT INTO [#HistoryOutput]
        ( [shopper_id], [skus] )        

        SELECT
            [shopper_id]
        , STUFF(( SELECT ', ' + ISNULL([a].[sku], '')
                                                            FROM [#ShopperSku_History_Load] [a]
                                                            WHERE [a].[shopper_id] = [b].[shopper_id]
                                                                FOR
                                                                        XML PATH('')
                                                        ), 1, 1, '') [skus]
    FROM [#ShopperSku_History_Load] [b]
        GROUP BY [shopper_id];

    SELECT
        [shopper_id]
      , [skus]
    FROM
        [#HistoryOutput];

END;

UPDATED ERROR

Exception from HRESULT: 0xC0202009
Error at Data Flow Task [OLE DB Source [1]]: 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: "Statement(s) could not be prepared.".
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 11.0"  Hresult: 0x80040E14  Description: "Incorrect syntax near 'shopper_id'.".
sql-server
stored-procedures
ssis
sql-server-2012
asked on Stack Overflow Jul 7, 2017 by erasmo carlos • edited Jul 7, 2017 by erasmo carlos

1 Answer

5

This error

The metadata could not be determined because statement 'EXEC XXXXXX' in procedure 'XXXXXX' contains dynamic SQL. Consider using the WITH RESULT SETS clause to explicitly describe the result set.

happens because the SP contains either a

  1. Temp table
  2. Dynamic SQL

Because of these dynamic elements, SSIS/SSDT has trouble getting the column metadata correctly. More info here. We need to help SSIS get that column metadata. Depending on your SQL Server version, there are two solutions.

enter image description here

For SSIS/SQL 2008, you can try setting FMTONLY OFF enter image description here

For SSIS/SQL 2012, you can wrap the stored procedure with a RESULT SET from SSIS. Try this ...

enter image description here

Other options include updating the Stored Procedure itself and adding a WITH RESULTS SETS clause. Or updating the stored procedure to return a table variable.

answered on Stack Overflow Jul 7, 2017 by Troy Witthoeft • edited Jul 10, 2017 by Troy Witthoeft

User contributions licensed under CC BY-SA 3.0