IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Inserting a row by copying value from existing row.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
vkisweet

New User


Joined: 28 Aug 2008
Posts: 2
Location: Bangalore

PostPosted: Wed Sep 24, 2008 5:47 pm
Reply with quote

Hello,
How can I select an existing record from the table and change the primary key value, other fields unchanged and insert back as a new record or replace the existing one.
Back to top
View user's profile Send private message
anil.csk

New User


Joined: 22 Oct 2007
Posts: 16
Location: Noida

PostPosted: Wed Sep 24, 2008 6:20 pm
Reply with quote

Use the query:

Insert into tablename (fields name) select fields names from table where primary = anything what wever you want..
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Sep 24, 2008 10:03 pm
Reply with quote

Hello,

And after the insert is successful, you need to delete the original row. . .
Back to top
View user's profile Send private message
Krishna Velamur

New User


Joined: 22 Aug 2008
Posts: 22
Location: Hyderabad

PostPosted: Thu Sep 25, 2008 11:23 am
Reply with quote

Hi,
There are two ways of doing it.

-> Insert a new record and delete the old record, as suggested by Dick.
-> Directly update using Update query (if there is no referential integrity), but in this case you need to reorg the index. This is not adviceble.

So the first solution is the best one. Corrections are welcome.
Back to top
View user's profile Send private message
feellikeneo

New User


Joined: 19 Mar 2007
Posts: 73
Location: Chennai

PostPosted: Thu Sep 25, 2008 12:14 pm
Reply with quote

Hi,

I have a similar query where in i need to insert a row into TABLE1 whose one of the column value should be from TABLE2. I used the below query,

Code:
INSERT INTO QUAL.TABLE1
VALUES
(  '05'
  ,'XXXXX'
  ,(SELECT TBL2.COL1
     FROM    QUAL.TABLE2 TBL2
     WHERE  TBL2.COL2 = 'YY')
  ,CURRENT TIMESTAMP
)

But the above query failed giving a SQL code of -4700 saying
ERROR: ATTEMPT TO USE NEW FUNCTION BEFORE NEW FUNCTION MODE

Could you please help me to over come the above error.
Please correct my query if anything wrong.

Appreciate your help on this.
Back to top
View user's profile Send private message
Venu Rao

New User


Joined: 01 Nov 2007
Posts: 4
Location: Westborough MA

PostPosted: Fri Sep 26, 2008 2:14 am
Reply with quote

here you go.

INSERT INTO TABLE1
SELECT
'YYYY' <---- PRIMARY KEY
,'ZZZZ' <---- PRIMARY KEY
,COL3
,COL4
,COL5
FROM TABLE1
WHERE
PMK COL1 = AAAA
AND PMK COL2 = BBBB
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Sep 26, 2008 3:04 am
Reply with quote

Hello Venu Rao,

I believe there may be some correction needed for the posted sql.

If you post the correction, i'll delete this reply and the original icon_smile.gif
Back to top
View user's profile Send private message
feellikeneo

New User


Joined: 19 Mar 2007
Posts: 73
Location: Chennai

PostPosted: Fri Sep 26, 2008 8:28 am
Reply with quote

Dick,

We are using DB2 V7.1 here.

Venu,

Thank you very much for your query. I was able to make it. Here is what I used,

Code:
INSERT INTO QUAL.TABLE1
VALUES
(  SELECT
   '05'
  ,'XXXXX'
  ,TBL2.COL1
  ,CURRENT TIMESTAMP
    FROM    QUAL.TABLE2 TBL2
    WHERE  TBL2.COL2 = 'YY')
)


Great piece of code !!
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Sep 26, 2008 8:47 am
Reply with quote

Hi Neo,

Good to hear it is working - thank you for letting us know icon_smile.gif

d
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Adding first / last acct numerber to ... DFSORT/ICETOOL 7
No new posts I want to append a record in an exist... CLIST & REXX 17
No new posts Panvalet - 9 Character name - Issue c... CA Products 6
No new posts Inserting variable length into output... JCL & VSAM 2
No new posts Inserting records in a file on a valu... DFSORT/ICETOOL 18
Search our Forums:

Back to Top