• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 852
  • Last Modified:

SCAN/REPLACE

I have a SQLSTMTSTR that has the entire SQL script and is built dynamically in the SQLRPGLEW program.  Once the build is complete, I have to SCAN for the WILDCARD CHARS "???"  and replace with the field name EMSCEM in ALL occurances in the SQLSTMTSTR.  I have to do this because there are multiple tables to join and the SCHEMA name has to precede EACH table name, so it will point to the correct DB on the UNIX server.
0
bcloring08
Asked:
bcloring08
  • 5
  • 5
  • 3
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi bcloring08,

Is there a question here?


Kent
0
 
bcloring08Author Commented:
Yes, I need to be able to SCAN and REPLACE the "???"  in the SQL script and replace the "???" with  a field name EMSCEM that has the MDM DB name in there.

Here is the idea that I am trying. I don't know if it will process ALL occurances or just the first one though, since I can't connect to the DB yet to ry it out....

C                   Z-ADD     0             Position          3 0    
C                   eval      Position = %scan('???':SqlStmtStr)      
C                   If        Position <> 0                          
C                   eval      SqlStmtStr = %replace(EMSCEM:SqlStmtStr:
C                             position:1)                            
C                   endif                                            




i.e.
SPR_V (this is the MDM DB view) AL1 (is the table name Alias)
           FROM  ???.SPR_V AL1, ???.TIME_FSC_DY_V AL2, ???.CP_DLY_SLS_V AL6,
???.CP_INV_LCN_V AL8, ???.BSN_UN_PDT_A_V AL9, ???.PDT_CNSGN_IND_V AL10, ???.BSN_
UN_LCN_V AL11                                                                  
0
 
Kent OlsenData Warehouse Architect / DBACommented:
What's your host O/S?
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
bcloring08Author Commented:
HOST O/S      =   AS400 Db2-400  
TARGET O/S  =   Oracle MDM DB on Unix server
0
 
Kent OlsenData Warehouse Architect / DBACommented:

If you can move the source to the unix box, changing it from there is almost trivial.

sed 's/\?\?\?/EMSCEM/g' oldfilename > newfilename



Kent
0
 
bcloring08Author Commented:
Kdo,

     Unfortunately, because this is an As400 BATCH job, moving the source to the Unix system is not an option.  

    The goal of this project is to connect to the Unix MDM DB, extract the inventory and sales information in the tables there, return to the AS400 with the SQL statement built, then I load it to the 852 EDI transaction set and send the information to the trading partners.  I think the only 2 pieces I am missing is CONNECTING to the UNIX from the iSeries and then once the SQL statement is built (SQLSTMTSTR)  then I have to replace the wildcard chars (???) with the Schema name in multiple locations in the script.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Dave's probably our most knowledgeable AS/400 geek, er guru.  Let's see if he can help.

I have no idea how you would connect to an Oracle database from the AS/400.  I don't even know if it can be done, but I suspect that it can.

If the data volume is modest, I'd consider firing up DB2 on your unix server and federating it to the AS/400 and the Oracle systems.  You can then run all of the SQL from the federated DB2 server and most of this will just go away.


Kent
0
 
tliottaCommented:
bcloring08:

This question should be in the RPG forum:

http://www.experts-exchange.com/Programming/Languages/Main-Frame/RPG/

However --

C                   eval      Position = %scan('???':SqlStmtStr)      
C                   If        Position <> 0                          
C                   eval      SqlStmtStr = %replace(EMSCEM:SqlStmtStr:
C                             position:1)                            
C                   endif                                            

That's a decent start. It will only replace a single occurrence, though, so some arranging is called for:

C                   eval      Position = %scan('???':SqlStmtStr)      
C                   If        Position <> 0                          
C                   dow       Position <> 0                          
C                   eval      SqlStmtStr = %replace(EMSCEM:SqlStmtStr:
C                             position:1)                            
C                   eval      Position = %scan('???':SqlStmtStr)      
C                   enddo
C                   endif

That's a little closer. Enclosing the inner statements in a dowhile-group and rechecking the %scan() will cause each subsequent occurrence to be found.

However...

C                   eval      SqlStmtStr = %replace(EMSCEM:SqlStmtStr:
C                             position:1)

...will be a problem because it explicitly replaces just a single [1] character and your search pattern is three characters [???] in length. Also, it's not clear what the data types are of EMSCEM and SqlStmtStr. If both are varying-length strings and the lengths are properly set and there's no likelihood of extending beyond the maximum length of SqlStmtStr, then simply changing [1] to [3] might work okay.

If these are fixed-length, however, then you _might_ have a problem when EMSCEM has trailing blanks. Perhaps use this instead:

C                   eval      SqlStmtStr = %replace(%trim(EMSCEM):SqlStmtStr:
C                             position:3)             <=== And remember the [3]

I think the %trim() function should work as the first parameter for %replace().

Tom
0
 
bcloring08Author Commented:
Thank you that worked perfectly. Now all I have to figure out is how to connect to MDM DB from iSeries, which is in another question I posed to the experts here.  Thanks again everyone!
Bob Loring
0
 
tliottaCommented:
Minor added note... Kent's mention of sed brings up regular-expressions and Qshell utilities. Those are accessible from ILE RPG, so _potentially_ useful. However, native bifs like %scan() and %replace() will perform better in almost every case. They're good to keep in mind for particularly tricky cases though.

Tom
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Tom,

My apologies in forgetting about you earlier.  I hadn't seen you for a while and had tried to defer to Dave, forgetting that you too are quite the resource for AS/400 related items.

  :)


Kent
0
 
tliottaCommented:
Kent:

No problem. Every once in a while, somebody has to work. I suspect that's where Dave is!

Tom
0
 
bcloring08Author Commented:
Thank you again Tom and Kent. I am just waiting for the EDW department to send me the JBDC Net drivers so I can connect from the iSeries to the Unix box.
Bob
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now