[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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

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
Alaska Cowboy
Asked:
Alaska Cowboy
  • 5
  • 4
3 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
Alaska CowboyAuthor Commented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
Alaska CowboyAuthor Commented:
>>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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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
 
Alaska CowboyAuthor Commented:
>>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
 
Alaska CowboyAuthor Commented:
I was told by a colleague to try "for each container". that sounds like something I'm used to . . .
0
 
Alaska CowboyAuthor Commented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your package.  -Jim
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now