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 > Is there a way to insert nonduplicate rows while duplicates getting dropped

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-21-05, 14:36
MahendraSetty MahendraSetty is offline
Registered User
 
Join Date: Apr 2004
Posts: 48
Smile Is there a way to insert nonduplicate rows while duplicates getting dropped

Hi,

I am trying to insert into a table by selecting certain columns from other table. The first table does contain some rows. The select produces some rows which are already there in first table and also produces some new rows. These new rows are what I want to insert into first table.

I am using an sql like

Insert into table1
select colA,colB,colC from table2

I get sql -803 as the table 1 is not empty and there is a unique index defined.

Is there a way to insert only the non duplicates from 'select colA,colB,colC from table2' ?

Thanks
Mahendra
Reply With Quote
  #2 (permalink)  
Old 01-21-05, 14:47
achiola achiola is offline
Registered User
 
Join Date: May 2002
Location: General Deheza, Cba, Arg.
Posts: 273
hi.

you're defined fk in some column?
Reply With Quote
  #3 (permalink)  
Old 01-21-05, 15:28
MahendraSetty MahendraSetty is offline
Registered User
 
Join Date: Apr 2004
Posts: 48
No but I do have a unique primary key in table1 and that prevents the dups.
Reply With Quote
  #4 (permalink)  
Old 01-21-05, 16:02
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Insert into table1 a
select colA,colB,colC from table2 b
where not exists (select a.colA from table1 where a.colA = b.colA)

This assumes that the primary key is colA.

I am not 100% sure this works on an "insert into select from" statement, or if it does work, which version is DB2 is required (maybe on 8.2 only or does not work at all on any version). But the subselect is valid syntax for pure select statement.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 01-21-05, 16:47
achiola achiola is offline
Registered User
 
Join Date: May 2002
Location: General Deheza, Cba, Arg.
Posts: 273
no primary key, foreing key in the destination table? or a column with not null?
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