If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Skip records that violate constraints and continue load

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-06, 03:14
db2hrishy db2hrishy is offline
Registered User
 
Join Date: Jun 2004
Posts: 115
Skip records that violate constraints and continue load

Hi

I need to populate one table from another.But the second table has constraints

i am doing it like like this

insert into table t2
select * from tabl t1

the problem is i want the insert to continue if there are records in t1 that will violate the constraints in t2.

The records that do not match the constraints should be ignored.My question is how do we do it in db2 ?

regards
db2hrishy
Reply With Quote
  #2 (permalink)  
Old 05-23-06, 03:45
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
You can EXPORT the data from the first table, and then LOAD it to the second table. All the rows will be loaded, but the table will be in check pending state. When you run the SET INTEGRITY statement, you use the option to discard the rows with RI errors.

The EXPORT and LOAD commands are documented in the Command Reference manual.

Another option is use a subquery to validate the foreign keys:

insert into table t2
select * from tabl t1 where exists (subquery that checks foreign key);
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390

Last edited by Marcus_A; 05-23-06 at 03:52.
Reply With Quote
  #3 (permalink)  
Old 05-23-06, 03:45
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
INSERT will not work.

consider using the LOAD-utility with ENFORCE CONSTRAINTS option.
Reply With Quote
  #4 (permalink)  
Old 05-23-06, 05:37
db2hrishy db2hrishy is offline
Registered User
 
Join Date: Jun 2004
Posts: 115
Hi Folks

Thank you very much.

I was just wundering if there was nay way to do this using sql/pl or something ?

regards
Hrishy
Reply With Quote
  #5 (permalink)  
Old 05-28-06, 06:05
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by db2hrishy
I was just wundering if there was nay way to do this using sql/pl or something ?
Sure: just insert the rows one by one and ignore the negative SQLcodes on the way.
The simplest way to do this would be with a cursor to iterate the rows returned by your SELECT.
Nevertheless, certainly when the number of rows is large, the LOAD solution is in far the better one.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On