Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Error handling Expand / Collapse
Author
Message
Posted Tuesday, April 16, 2013 7:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 7:41 AM
Points: 30, Visits: 26
First off, my appologies, I am a newbie to programming and SQL.

Setup:
I have an SSIS package that inserts rows to a database. On occasion I get a constraint error on one of my reference tables. So far it is because of missing data.

What I want to do:
I want to redirect the row, grab the data and insert it into the reference table then reprocess the row.

Questions:
A) is this possible? I have looked at doing a lookup task prior to the row being inserted to the database. I have also looked at using a sql task in my error handling, but having issues with the code (see B).
B) I need help with the code. How do I pull the information from that single row then insert it into the database? Do I use a variable? Is this a select statement?

Any help would be greatly appreciated.

Thank you

Brian
Post #1442750
Posted Sunday, April 28, 2013 10:43 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Sunday, October 19, 2014 3:52 PM
Points: 364, Visits: 385
I would set up the SSIS package to insert into a staging table first. Then use a SQL task to check for references in the staging table that does not yet exist, and insert the references. Use something like a left outer join check:

insert into reference_table (reference_id, reference_description)
select s.reference_id, 'some description'
from staging_table s
left join reference_table r on r.reference_id = s.reference_id
where r.reference_id is null

After the references are resolved, copy from the staging table to the actual table being loaded.
Post #1447361
Posted Monday, April 29, 2013 6:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:15 AM
Points: 5,245, Visits: 12,162
brian.geregach (4/16/2013)
First off, my appologies, I am a newbie to programming and SQL.

Setup:
I have an SSIS package that inserts rows to a database. On occasion I get a constraint error on one of my reference tables. So far it is because of missing data.

What I want to do:
I want to redirect the row, grab the data and insert it into the reference table then reprocess the row.

Questions:
A) is this possible? I have looked at doing a lookup task prior to the row being inserted to the database. I have also looked at using a sql task in my error handling, but having issues with the code (see B).
B) I need help with the code. How do I pull the information from that single row then insert it into the database? Do I use a variable? Is this a select statement?

Any help would be greatly appreciated.

Thank you

Brian


There is more than one way to solve this. Here are three:

1) (Pure SSIS)
Use a lookup task to check for existence. When matched then continue as currently. When not matched:
a) Insert a suitable row into the reference table using a OLEDB command
b) Insert to child table as usual.

2) (Hybrid)
Use a lookup as above. When matched, all OK. When not matched
a) Direct to a staging table
b) At the end of the dataflow, run a stored proc to process and insert the rows in the reference and staging tables as required.

3) (Use SSIS as little as possible)
Use the solution suggested by Mansfield.

I would expect (2) to be the fastest, (1) is the most elegant and (3) is probably easiest to set up, maintain and troubleshoot - for SQL folks anyway. In my opinion, of course.

One problem with (1) is that you cannot use a cached lookup - so performance is likely to be average at best.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1447477
Posted Monday, April 29, 2013 6:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 7:41 AM
Points: 30, Visits: 26
My thanks to the two responders. Both suggestions look very helpful.

Brian
Post #1447486
Posted Wednesday, May 1, 2013 11:20 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, November 3, 2014 4:30 PM
Points: 4,574, Visits: 8,366
Mansfield (4/28/2013)
I would set up the SSIS package to insert into a staging table first. Then use a SQL task to check for references in the staging table that does not yet exist, and insert the references. Use something like a left outer join check:

insert into reference_table (reference_id, reference_description)
select s.reference_id, 'some description'
from staging_table s
left join reference_table r on r.reference_id = s.reference_id
where r.reference_id is null

After the references are resolved, copy from the staging table to the actual table being loaded.


From my experience WHERE NOT EXISTS always perfoms better, at least not worse, than LEFT JOIN with IS NULL check:

insert into reference_table (reference_id, reference_description)
select s.reference_id, 'some description'
from staging_table s
WHERE NOT EXISTS (select * from reference_table r
where r.reference_id = s.reference_id)

Post #1448691
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse