Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using built-in copy command without duplicate records

Status
Not open for further replies.

xiancmc

Programmer
Jul 11, 2006
6
CN
I need to copy records from the same file but on another library. It's like copy lib1/file1 to lib2/file1, but lib2/file1 is already existing and other records from lib1/file1 are already in lib2/file1. So what I'm hoping to do is to copy only those that are not existing in lib2/file1. Both files have the same key fields (eg. field1, field5, field8)

I hope there's a way to do this without doing a program like CL and etc.

I would highly appreciate the help.

Thank you.
 
A simple sql stm will do the trick.

Code:
Insert into lib2/file1 
( Select lib1/file1 a 
    Exception Join lib2/file1 b on a.field1 = b.field1
    and a.field5 = b.field5 and a.field8 = b.field8 )
 
Can't update the previous post. Must read

Code:
Insert into lib2/file1
( Select a.* from lib1/file1 a
    Exception Join lib2/file1 b on a.field1 = b.field1
    and a.field5 = b.field5 and a.field8 = b.field8 )
 
Thanks Mercury2, but I was hoping not to use SQL as this will make me create an object for the sql code. I just want to make it much simplier like using CPYF(not familiar if there's an option to do the trick here).

In a job scheduler I have a job that looks like this :

Seq Command
10 CLRPFM FILE(PFILE1)
20 CALL PGM(PGMSQL) PARM('parmA' 'parmB')
40 CALL PGM(PGMC) PARM('parmA' 'parmB')

50 /* copy code here */

60 CALL PGM(PGMCL)


Thanks in advance.
 
I don't think you could make it "simpler" than that. You don't need another object. But you would need to put the SQL code into a source member, then use RUNSQLSTM to execute it at the proper time.

Other option is to use a very simple RPG pgm. I don't think there is any OS command to do what you want.
 
In CL. yopu can also do a CPYF with MBROPT(*UPDADD). This will replace any records with the same key values, and add the ones that don't exist. It may not be what you want, though.

Solum potestis prohibere ignes silvarum.

 
Thanks flapeyre, actually this should work fine. But my next problem is that the master file I'm using is not using UNIQUE key fields.

Anyway, the next simplest thing to do is SQL.

Still, if incase you all guys ran across with a solution to this, please...let me know.

Many thanks.
 
Yes, I should have noted that using *UPDADD on CPYF requires unique key values. In that case, you might have to either use SQL or write a small RPG program to add your records (SETLL with the key and dont't write the new record if %FOUND).

Solum potestis prohibere ignes silvarum.

 
I've just learnt that
CPYF FROMFILE(LIB1/FILE1) TOFILE(LIB2/FILE1) MBROPT(*ADD) ERRLVL(*NOMAX)
will not insert the lines with keys already existing (duplicates) in the tofile. So maybe that one will fit your needs more .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top