Embedded SQL using quotes to build field with wildcard

25 pts.
Tags:
AS/400
RPGLE
SQL
How do I set up a cursor in RPGLE to select where a field is LIKE a character field using quotes with a wild card? The character field is a variable declared and built using a field in the program.  I have tried field LIKE :newfield with newfield setup as '%ch%' (single quotes). I tried field LIKE :newfield with newfield setup ''%ch%'' (double quotes). The SQL runs but returns SQLCODE of 100. When I run the SQL statement in a SQL session, records are returned as expected so I know data exists. How do I correctly add the quotes?

Answer Wiki

Thanks. We'll let you know when a new response is added.

I usually create a variable length string for the SQL statement -

DsqlStatement     S          10000a   Varying

Create constants as follows -

D percent         C                   ‘%’

D quote           C                   ””

Build the SQL command -

sqlStatement = ‘SELECT *  FROM MYFILE WHERE ITEM LIKE ‘+quote+%Trim(myitem)+percent+quote+’ FOR READ ONLY';

Use the statement to set up the cursor

EXEC SQL PREPARE STATEMENT_01 FROM :sqlStatement;       
EXEC SQL DECLARE MyCursor SCROLL CURSOR FOR STATEMENT_01;
EXEC SQL OPEN MyCursor;

etc.

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • ToddN2000
    We use something like this.
    /EXEC SQL                                                         
    +    DECLARE OHPO  CURSOR FOR                                     
    +       SELECT ORD#, BILLTO, PO#, SALESMAN
    +       FROM  OH                                                  
    +       WHERE HOLD = 'E'                        
    +       AND PO# LIKE '%' concat Trim(:INPO) concat '%'         
    
    /END-EXEC                                                         
    /EXEC SQL                                                         
    +    OPEN OHPO                                                    
    /END-EXEC 
    15,605 pointsBadges:
    report
  • cblanch

    I am using RPG Free and tried the Prepare method.  It worked!  As you may or may not have surmised, using Like with a field that included the quotes did not work.  I didn't think of the way either of you set it up.  That was exactly what I needed.

    Thanks for the help.

    25 pointsBadges:
    report
  • ToddN2000
    glad we could help.
    15,605 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following