SSIS – Using SSIS Variable within a SQL Script with Temp Tables – Error at Data Flow Task : No Column information was returned by the SQL Command
Below details what I was trying to do within SSIS and how I got it to work.
Overview
- I was using a SQL Script which contained some temp
tables which was used in the result set. -
When I then put this into an OLE DB Source I would get the error shown below:
-
I then tried to see if it would work when I created a SQL Command from Variable.
- And this would result in the same error as above.
-
If I did click on OK, I would then the following in the Columns window as shown below:
- So below is a solution which I found to work to get the data from a SQL Query that uses temp tables.
Example Data
For our solution below we are going to use the following sample data.
-
The first
variable that we are going to be using will be for a DayNumber- NOTE: This is because we want to go back in time, so we want to go back and loop through the past 10 days.
-
The second
variable that we are going to be using is our actual SQL Query.- So this will be very simply put with the following below:
- NOTE: This is a very simple
query and I want to just use this as an example. I am certain in most of your scenarios it will be a much more complex query that is giving you the above error within SSIS.
Solution
- The first thing that you will need to do, is to create an ADO.NET Source.
-
Next you will need to ensure that you have two
variables created- The first
variable that you want to pass into your SQL
Statement - The second
variable is your SQL query.
- The first
-
As with our example above we created the following variables within SSIS
-
From the above you will see that we have created some additional
variables as explained below:- DayNumber is the day number variable that we want to pass to our SQL Query.
- StartNumber is the starting variable in our For Loop Container
- EndNumber is the ending variable in our For Loop Container
- Query_GetDayNumber is the query which is going to extract our Day Number and put this into a variable.
- Query_ExtractData is the SQL Query where we are going to extract our data including passing the variable.
-
-
Next we will create and configure our For Loop Container with the following:
- NOTE: This is so that we can then know when to exit our of our For Loop Container
- NOTE II: Typically, you will have your StartNumber and EndNumber
populated by an Execute SQL Task so that it can always be dynamic.
-
Next we will configure our variable for the Query_GetDayNumber so that it gets populated with the correct details as it loops
through each time.-
Click on Variables, then where we have the Query_GetDayNumber
click on the Ellipses button on the right
hand side under
Expression - This will then open the Expression
Builder
Window -
Now as with our example, we know that our
number will always start at 1 and the DayNumber will always be the same as the StartNumber.- NOTE: There will be situations where you will need to run an actual SQL Query against your data to extract the required information.
-
And we put in the following:
- NOTE: The reason for converting it to a string is because our StartNumber
variable is defined as an Int32. - You can then click on the Evaluate
Expression to make sure that it is working.
- NOTE: The reason for converting it to a string is because our StartNumber
- Then click Ok.
-
-
Next we will need to configure our variable for the Query_ExtractData, so that we can then pass our above variable within our SQL Query by doing the following:
-
Click on Variables, then where we have the Query_ ExtractData
click on the Ellipses button on the right
hand side under
Expression - Now we will put in our SQL Query from above, along with the variable as shown below:
-
NOTE: If you have a look at the above
syntax you will see that we have put our variable
DayNumber into our Expression.- You can then click on the Evaluate Expression to make sure that it is working.
- Then click
Ok.
-
-
Next create an Execute SQL Task and configure it with the following below so that it will be used to populate our DayNumber
variable.- Go into the Properties and configure the General
page with the following below: -
NOTE: A few quick things to note on the above:
-
We have set the Result Set to Single Row
- This is to allow our variable to be returned into a result set.
-
Our SQLSourceType has been set to Variable.
- This is so that every time the For Loop Container runs and loops through it will then get the new value.
-
SourceVariable
- This has been selected from the drop down and will be our query which we created earlier.
- Which on each execution of the For Loop Container will go and get the values we require.
-
-
Then click on the Result Set and put in the following:
- Then click
Ok. - Now drag this into your For Loop Container
- Go into the Properties and configure the General
- Next drag in a Data Flow Task and rename it to Extract Data
- Then double
click and go into your Data Flow Task -
Now Drag in an ADO NET Source.
- Then ensure that you have not selected the ADO NET Source and have clicked on the sheet within SSIS, and go into the properties.
- Next to Expressions
click on the Ellipses
Button -
Under Property click on the Drop Down and select the following as shown below
- Then click on the Ellipses next to Expression.
-
Now in the Expression Window put in the following variable as shown below:
- NOTE: From the above you will see that this is our Variable query that we created and configured in step 6 above.
-
Click on the Evaluate
Expression and you will then see the actual query below in the Evaluated value:- NOTE: In the above it has gotten the number 12 from the default
value in our Variable for DayNumber.
- NOTE: In the above it has gotten the number 12 from the default
- Then click Ok.
-
Now go back into your ADO NET Source.
- Ensure that you have got the correct connection to your data source.
- Then under Data access mode
change this to SQL Command - Then you should see your query populated below as with our example:
- NOTE: It got this query
information from our Expression, which in turn got the information from our variable.
- You can then click on Columns to see that the query
runs and gets the required
information. - Then click Ok.
-
Then the final step is to then link it to your destination table.
- NOTE: You can use either an OLE DB Destination or an ADO NET Destination.
-
Next make sure that you drag your Data flow task into your For Loop Container
- Then ensure that you place the Success
Precedence
Constraint
between your Execute SQL Task and your Data Flow Task as shown below.
- Then ensure that you place the Success
- Now you can run your SSIS package and it should work
successfully.
This post was published to myfriendjoobs at 1:57:59 PM 8/5/2015
SSIS – Using SSIS Variable within a SQL Script with Temp Tables – Error at Data Flow Task : No Column information was returned by the SQL Command
Hi Gilbert,
you can also solve the issue putting your query in a stored procedure (e.g. usp_MyQuery_0) and than creating a new stored procedure like this one:
CREATE PROCEDURE usp_MyQuery
AS
EXEC usp_MyQuery_0
WITH RESULT SETS ((
[DATETIME] datetime NULL,
VariableDate datetime NULL
))
In this way you can call the last stored procedure in your OLE DB Source with an EXEC usp_MyQuery and all will work fine.
Regards.
Hi there.
Thanks for your comment.
That does indeed work but in my situation I couldn’t create a stored proc on the source system. Due to constraints on the source database.
So this was the way that I got it to work.
Ok. I usually create a “Helper” database in my BI stack that simply get data from the source system through views and linked servers. In this Helper I also create all the objects I need for the loading phase (e.g. stored procedures).
By the way, good to know that ADO.NET Source do a good job in these cases.
Thank you!
Hi there.
That will definitely work. But it also wanted to loop through all the data so that I could get it all on each load.
Glad that you found it useful.