SQL Server 2016 SSIS get cursor from stored procedure

纵饮孤独 提交于 2020-01-06 19:30:06

问题


I am using SQL Server 2016.

  1. I have a stored procedure GET_RECORDS that takes input parameters for filter and outputs a CURSOR parameter

  2. I want to get this cursor in my SSIS package

  3. I had created data flow task, OleDb source and variables for parameter values. Then mapped parameters

Params mapping screen

but when I wanted to save the component - I got an error

error screen

I tried to add clause WITH RESULT SETS with some dummy columns, but my procedure doesn't return any result set

What am I doing wrong?

Any advices will be helpful.

Thank you.

With regards, Yuriy.


回答1:


The source component is trying to determine what columns and types will be returned. Because you are using dynamic SQL the metadata can change each time you run it.

With result sets allows you to define the data being returned but should only be used if you are guaranteed to have those results every time you execute.

EDIT: I create a connection and run the command so that it populates a data table. Then I put the column headers into a string array. There are plenty of examples out there.

Then I use the following function to create a destination table. Finally I create a datareader and pass that to the .Net SqlBulkCopy. Hope this helps.

private void CreateTable(string TableName, string[] Fields)
        {                

            if (TableExists(TableName) && Overwrite)
            {
                SqlCommand = new SqlCommand($"Drop Table [{TableName}]", SqlConnection);
                SqlCommand.ExecuteNonQuery();
            }

            string Sql = $"Create Table [{TableName}] (";

            int ColumnNumber = 1;
            foreach (string Field in Fields)
            {
                string FieldValue = Field;
                if (! HasHeaders)
                {
                    FieldValue = "Column" + ColumnNumber;
                    ColumnNumber++;
                }
                Sql += $"[{FieldValue}] Varchar(8000),";
            }
            Sql = Sql + "ImportFileID Int, ID Int Identity(1,1) Not Null, Constraint [PK_" + TableName + "] Primary Key Clustered ([ID] Asc))";

            SqlCommand = new SqlCommand(Sql, SqlConnection);
            SqlCommand.ExecuteNonQuery();
        }



回答2:


Use ado.net source instead of oledb source, define a simple select and get the columns you wish to return. Now you can define expresión in the dataflow properties.

Search ado.net source dynamic sql

:)




回答3:


try to return the records and use foreach in ETL instead of cursor

https://www.simple-talk.com/sql/ssis/implementing-foreach-looping-logic-in-ssis/

I think you can do it from a simple way, but I don't know what you are you doing, exactly...



来源:https://stackoverflow.com/questions/37483535/sql-server-2016-ssis-get-cursor-from-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!