Solved

SSIS, how to take a field from source query and move to a variable for later us

Posted on 2013-10-31
9
264 Views
Last Modified: 2016-02-11
I have a simple task but can't figure out the mechanics.

I am successfully moving data from point A to point B, using a Sql command from variable, this works great.

But now I want to take one of the columns from this query and populate another variable so I can use that in another subsequent data flow.

So how do I do this ? Copy column didn't work, and Derived Column looked promising, but that didn't work as I thought it might either.

My source query returns 1 row, and I want to take the value of one of the columns and populate a variable for later use.

Here's the data I'm trying to move

HDR
    Detail
        Detail Child record
Trailer

I think the issue is I don't understand how SSIS works . . .

In oracle, I would write a cursor for HDR, maybe it pulls 5 records.

So I fetch first record, insert it to the dest table HDR in a different d.b.
  then, while holding this record,
      I pull all related detail records that have the Header.UniqueKey as FK
               while processing each detail record, I similarly look for children of the detail
      after all details are inserted, I'll then use the header in context and get the trailer
      then fetch the next header record and begin the above sequence again.

So how do I accomplish in SSIS ?
0
Comment
Question by:Alaska Cowboy
  • 5
  • 4
9 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
Don't think you can do it in a data flow, but you can always create an SQL Task afterwards that goes something like this

In the Execute SQL Task Editor..
Set the 'Result Set' to Single Row,
SQL Statement is <air code> SELECT your_column FROM your_table WHERE whatever_results_in_a-single_row
click on Result Set tab, then add your parameter.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
Comment Utility
Jim,

ok, so you're saying I'm not going to use similar procedure logic as in an Oracle (or Sql Server) cursor.

but surely this is a standard scenario, isn't it ?

I kind of see what you are saying, but not really getting the gist of it

Say I had 5 header records, and various details and associated trailer records.

Do I move all five records first ? and then move to the detail records ?

So after moving the 5 header records, I could then do an Execute Sql Task that pulls in all five seq_no's to a full result set (and place in a variable) ? and then my detail query says "select * from detail_table where seqno in ('full_result_set_variable') ???
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
Comment Utility
In SSIS this will be the same process regardless of Oracle or SQL server.

>that pulls in all five seq_no's to a full result set (and place in a variable
The problem here is that SSIS variables are scalar (i.e. single value) unless it's an Object, and what you are proposing is to take a set, and grab a column which may or may not have the same value in every row, and assign it to that variable.

>select * from detail_table where seqno in ('full_result_set_variable')
Now THAT would be a lookup transformation, which looks up the list from Oracle, and is joined to the earlier SQL set on whatever rows match.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
Comment Utility
>>In SSIS this will be the same process regardless of Oracle or SQL server.
- ok, well, that sounds promising

but I think I need to just scratch everything and ask, how do I do this ?

say I have five header records and various details for each
HDR
    Detail
        Detail Child record
Trailer

does SSIS insert all five header records first ? or am I looping through one header record at a time and then getting the details and finally the trailer ? If so, I think I'm back to my original question, how do I grab the seqno of each header record as it's being processed so I can get the associated details and trailer ?

(I could knock this out in 10 minutes in oracle, but I'm trying to use SSIS . . . )
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
>does SSIS insert all five header records first ?
SSIS can't handle different row types in a data flow, such as headers and footers, unless these rows are distinct enough that you can pick them off with some kind of conditional split, or custom code.

This really must be done after the data is loaded into a staging table, editing it using a script task and custom T-SQL.

So, I declare dibs on 'it can't be done' as the answer to your question as it is defined.

>(I could knock this out in 10 minutes in oracle, but I'm trying to use SSIS . . . )
If this is correct, then I'd go with the Oracle solution.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
Comment Utility
>>If this is correct, then I'd go with the Oracle solution.
- except that SSIS is the tool of choice now plus I need to learn it (to keep my job).

I understand SSIS can't handle different row types in a data flow, but I was planning on a  separate data flow for each record type.

hmmmm . . .

what about this

Execute Sql Task
- get last_pull_from_date

Data Flow 1
- select * from header where insert_date > last_pull_from_date (5 records)

Data Flow 2
- select * from
     header a inner join detail b on a.hdr_seqno = b.hdr_seqno
  where hdr.insert_date > last_pull_from_date (XXXX records)

Data Flow 3
- select * from
     header a inner join trailer b on a.hdr_seqno = b.hdr_seqno
  where hdr.insert_date > last_pull_from_date (XXXX records)

--------
this only works because I am moving data as is, so the seqno's match up. So the seqno on all detail records is the same between source and dest. If I was inserting a seqno based on destination, then I couldn't do the above.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
Comment Utility
I was told by a colleague to try "for each container". that sounds like something I'm used to . . .
0
 
LVL 1

Author Closing Comment

by:Alaska Cowboy
Comment Utility
I ended not needing a "for-each loop container", but thanks for the tips, this helps me get the jist of how SSIS operates.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Thanks for the grade.  Good luck with your package.  -Jim
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

My client sends a request to me that they want me to load data, which will be returned by Web Service APIs, and do some transformation before importing to database. In this article, I will provide an approach to load data with Web Service Task and X…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

744 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

17 Experts available now in Live!

Get 1:1 Help Now