Solved

SSIS Result Set and Stored Procedure

Posted on 2016-07-22
5
15 Views
Last Modified: 2016-07-25
Hello

I have a SSIS SQL Task-1that calls a stored procedure with 3 columns - Name, Number and ID with multiple rows which is then stored in an object variable for another SQL Task-2 to use the ID as an input.

Task 1 Output:
name, number, ID
A,2,3
B,3,4
C,5,6

The foreach loop workflow is:

- Truncate stage table,
- load data for name A,
- stored procedure to get value for name A and load into destination table,
- move data file for name A to archive....

then repeat the process for  name B, then name C one after the other..

Does anyone know the best way that I can get SQL Task-2 to use the ID's from Task-1 one by one and not use all of ID's at the same time.

As it is now, task 2 is processing all of the ID's and creating values to the destination table as:

Name, ID, value
A, 3, 300
B, 4, 0
C, 6, 0

Values for Name B and C is appearing 0 Because data for both of them is not loaded because the foreach loop is not repeating the workflow from step 1 for each of them.
0
Comment
Question by:barkome
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:DcpKing
ID: 41726037
Have you tried arranging things so that Task 1 only emits records for A and B and not C? If you do that, does Task 2 still provide the same results? In that case I'd suggest that your problem is with Task 2 and not your SSIS.

hth

Mike
0
 

Author Comment

by:barkome
ID: 41726142
Yeah, I did so...

I have a stored procedure that creates a data set with ID's, which is then sent to a Foreach Loop ADO Enumerator. Within the Foreach Loop are two stored procedures: The first SP, works as expected, but the second one fails, because its receiving just the first ID presented, and not looping through like the first SP.

Below is a image of the flow.

Foreach-Loop.png
The logic is, 3 ID's are retrieved 1,2 and 3. Task 1 which is the first SP, will execute with ID 1 as an input, and when complete pass that same ID (1) to Task 2 for the second SP to execute with.

Once this loop is complete, the logic/workflow will start again from task 1 with ID 2, and once complete, start again from task 1 again but this time with ID 3.
0
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 41726771
Well, what I've done is create a very small SSIS package that does roughly what you're trying to do, excepting, of course, that it has none of your data or tables. Make yourself the three procs below and the SSIS package. I believe I've included everything you need, and it should just work! Then you should be able to compare it with your version to see where you're maybe going wrong.


The whole thing is in pic 1. Pics 2 and 3 show how data from the first procedure (Barkome_0) is brought into an Object for use by the ForEach loop.
alter procedure Barkome_0
as
/*
exec Barkome_0
*/
begin
	set nocount on
	declare @ta table (Name varchar(8), Number int, ID int)
	insert into @ta(Name, Number, ID)
		values
		('A',2,3),
		('B',3,4), 
		('C',5,6)
	select ID from @ta
End

Open in new window


Then there are pictures showing the general and result set dialogs for Task 1 and Task 2, and a list of the variables.

The script tasks are simply there to produce pop-up boxes telling you what the variables are ... the code is just this:
public void Main()
         {
MessageBox.Show(Dts.Variables["User::YourVariableNameHere"].Value.ToString(), "Message Box Title Here");
         }

Open in new window


The variables for the commands for the two tasks are these:

"exec Barkome_1 " +  (DT_WSTR, 3) @[User::intID]
"exec Barkome_2 " +  (DT_WSTR, 3) @[User::intID] + ", " +  (DT_WSTR, 3) @[User::intNumber]

and the code for the two tasks is as follows:
ALTER procedure [dbo].[Barkome_1]
(
	@intID int
)
as
/*
exec Barkome_1 4
*/
begin
	set nocount on
	declare @strName varchar(8), @intNumber int
--	----------------------------------
	declare @ta table (Name varchar(8), Number int, ID int)
	insert into @ta(Name, Number, ID)
		values
		('A',2,3),
		('B',3,4), 
		('C',5,6)
	select	@strName = Name, 
			@intNumber = Number
	from @ta 
	where ID = @intID
--	----------------------------------
	select @strName as 'Name', @intNumber as 'Number', @intID as 'ID'
End

Open in new window

alter procedure Barkome_2
(
	@id int,
	@number int
)
as
/*
exec Barkome_2 4, 3
*/
begin
	set nocount on
	declare @ta table (ID int, Constant int)
	insert into @ta(ID, Constant)
		values
		(3, 29),
		(4, 42), 
		(6, 17)
	declare @result int 
	set @result = (select Constant from @ta where ID = @id)
	set @result = @result * @number
	select @id as 'ID', @result as 'Value'
End

Open in new window

Screenshot-from-2016-07-24-17-10-24.png
Screenshot-from-2016-07-24-17-10-58.png
Screenshot-from-2016-07-24-17-11-15.png
Screenshot-from-2016-07-24-17-17-55.png
Screenshot-from-2016-07-24-17-18-17.png
Screenshot-from-2016-07-24-17-18-44.png
Screenshot-from-2016-07-24-17-19-00.png
Screenshot-from-2016-07-24-17-19-48.png
0
 

Author Comment

by:barkome
ID: 41726980
Thanks for this Task 1 works perfectly well when Task 2 is not part of the workflow, loading the right values one after the other.

Did some digging around and noticed that it had to do with the data type. applied the CAST syntax to the affected columns and it suddenly worked as expected, but only for it to duplicate a few records with a default value of 0.00
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 41727476
Take a look at my Task 2 and compare it with yours. Is yours getting the data it needs? If not, have some way of recording what it does get (write the values out to a table) and look at them there. Look at the way that I'm splitting out the values that are passed from task to task into variables. Are you trying to keep them in an object or something? Putting them in variables whose value changes each time around the loop is a lot more reliable and easier to debug.

Mike
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is my first ever Article on EE or elsewhere; therefore, please bear with me if I have some discrepancies in my writing. I read many articles and questions related to "how to pass values to SSIS packages at run-time?"  Hence, this common ques…
In couple weeks ago, I encountered an extremely difficult problem while deploying 2008 SSIS packages to a new environment (SQL Server 2014 standard).  My scenario is: We have one C# application that is calling 2008R2 SSIS packages to load text fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now