Solved

SSIS Result Set and Stored Procedure

Posted on 2016-07-22
5
19 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From time to time, for debugging and troubleshooting your flow at run time you’ll need to check if the variable has the correct value or not, there’re several ways to check for the value of the variables inside the flow, You can add Break Points, a …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.

726 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