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 > INSERT from one to another Table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-10, 15:57
asriva26 asriva26 is offline
Registered User
 
Join Date: Feb 2010
Posts: 5
INSERT from one to another Table

Hi;
I have two tables exactly same but in two different DB schema. the Table_1 has a primary key SU_ID which gets assigned by system while submitting the value thru online application. I am doing an INSERT INTO command like:

INSERT INTO TABLE_1 SELECT * FROM TABLE_2.

the problem is that TABLE_2 has SU_ID which is unique and TABLE_2 has same value.

How to resolve this issue? Please advice?
Reply With Quote
  #2 (permalink)  
Old 02-02-10, 16:09
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Why would you want to insert duplicate values into table_1?
Reply With Quote
  #3 (permalink)  
Old 02-02-10, 16:27
asriva26 asriva26 is offline
Registered User
 
Join Date: Feb 2010
Posts: 5
TABLE_1 has old data, TABLE_2 has the updated datab but the SU_ID is unique to each table and it is primary key
Reply With Quote
  #4 (permalink)  
Old 02-02-10, 16:42
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Please try to explain what is it that you wish to accomplish. Do you want to update rows in table_1 with new values in table_2, or add rows from table_2 to table_1? In the latter case you will have to generate unique key values yourself.
Reply With Quote
  #5 (permalink)  
Old 02-02-10, 16:56
asriva26 asriva26 is offline
Registered User
 
Join Date: Feb 2010
Posts: 5
Sorry for the confusion. Let me explain what I want.

I have TABLE_1 which has updated value where SU_ID is primary key. the value in SU_ID is system generated 12 byte number. This table has tons of rows.

I have another TABLE_2 which has exactly same structure but has different value(old data).

How come I insert the new data into TABLE_2 from TABLE_1?

I know that INSERT INTO TABLE_2 SELECT * FROM TABLE_1 will not work because TABLE_2 might have values in SU_ID which may be in TABLE_1.

I was thinking that if I first find out a Max(SU_ID) in TABLE_2 and then keep increamenting by one for the next row then it may work but I don't know how?
Reply With Quote
  #6 (permalink)  
Old 02-02-10, 17:10
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Code:
insert into table_2 (su_id, ...) 
select 
 row_number() over () + <maximum su_id value>, 
 <rest of columns> 
from table_1
Reply With Quote
  #7 (permalink)  
Old 02-02-10, 17:58
asriva26 asriva26 is offline
Registered User
 
Join Date: Feb 2010
Posts: 5
Many Thanks.
while running my query in Mainframe using QMF tool:

SELECT
ROW_NUMBER() OVER () + <MAXIMUM SU_ID>,
OVERRIDE_FLAG,EFFECTIVE_DATE,END_DATE,PARM_VALUE
FROM MTVDV1.JOB_PARM_DETAIL;

it is throwing an SQL error like "SQL error at or before < (line 2, position 25)."


Can you explain why?
Reply With Quote
  #8 (permalink)  
Old 02-02-10, 18:07
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Could be something like this:

INSERT INTO TABLE_1
SELECT * FROM TABLE_2
where SU_ID not in (select SU_ID from TABLE_1)
;
Lenny
Reply With Quote
  #9 (permalink)  
Old 02-02-10, 20:14
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by asriva26 View Post

Can you explain why?
Can you explain why you have replaced "<rest of columns>" with "OVERRIDE_FLAG,EFFECTIVE_DATE,END_DATE,PARM_VALUE" , but only removed "value" from "<maximum su_id value>"?
Reply With Quote
  #10 (permalink)  
Old 02-02-10, 22:12
asriva26 asriva26 is offline
Registered User
 
Join Date: Feb 2010
Posts: 5
I thought that "<rest of columns>" should be replaced by the actual columns name in the table. in the same way I wrote for "<maximum su_id value>" and removed "value"

Is this is a mistake? do I need to run the SQL the way you have written the sql?
Reply With Quote
  #11 (permalink)  
Old 02-03-10, 08:15
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
MERGE instead of INSERT

Quote:
Originally Posted by asriva26 View Post
I thought that "<rest of columns>" should be replaced by the actual columns name in the table. in the same way I wrote for "<maximum su_id value>" and removed "value"

Is this is a mistake? do I need to run the SQL the way you have written the sql?
You can use MERGE.

Cara.
Reply With Quote
  #12 (permalink)  
Old 02-03-10, 08:23
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by asriva26 View Post
I thought that "<rest of columns>" should be replaced by the actual columns name in the table. in the same way I wrote for "<maximum su_id value>" and removed "value"
How about replacing "<maximum su_id value>" with the actual maximum su_id value?
Reply With Quote
  #13 (permalink)  
Old 02-04-10, 15:01
schintala schintala is offline
Registered User
 
Join Date: Apr 2005
Location: USA
Posts: 119
What is your DB2 version on z/OS? If you are in V9.1 it can be done through merge.
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