• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Executing Dynamic Calculations with Embedded SQL

    November 10, 2004 Michael Sansoterra

    The code for this article is available for download.

    Every so often I encounter an application where an advanced user wants the ability to maintain a formula. Further, the formula may change every so often, so that building the formulas into the program is undesirable. This is a difficult situation in the RPG world, since the RPG language has no ability to evaluate a string expression at runtime.

    Here’s an example:

    Formula='Qty * Price * (1 + Case When Qty > 100 Then .10 Else 0 End)';
    

    Fortunately, SQL can handle this dynamic calculation.

    This calculation can be done by following these steps:

    1. Build a string containing an SQL column expression.

    2. Replace the predefined parameters in the string with their runtime values. (Users must know beforehand a list of variables they’re able to use in a formula.)

    3. Embed the column expression in a SELECT statement that returns a single row.

    4. Use SQL’s Prepare statement to convert the SELECT statement into an executable form.

    5. Open a cursor from the prepared SELECT statement.

    6. Fetch the data into a host variable.

    7. Close the cursor.

    These steps are outlined in RPG program CALCR. The program uses subprocedure Calculator to dynamically calculate a value from a string expression. Here’s a sample of how the Calculator subprocedure can be used:

    /Free
        Amt=23.50;
        Qty=10;
        Expr='AMT*CASE WHEN QTY>5 THEN .1 ELSE .05 END';
        Expr=Replace(Expr:'AMT':%Char(AMT));
        Expr=Replace(Expr:'QTY':%Char(QTY));
    
        // Discount evaluates to 2.35  (23.50 * .01)
        Discount=Calculator(Expr);
    
        Amt=50.00;
        Qty=1;
        Expr='AMT*CASE WHEN QTY>5 THEN .1 ELSE .05 END';
        Expr=Replace(Expr:'AMT':%Char(AMT));
        Expr=Replace(Expr:'QTY':%Char(QTY));
    
        // Discount evaluates to 2.50  (50.00 * .05)
        Discount=Calculator(Expr);
    
    /End-Free
     

    There are a few things to note.

    • Typically, dynamic formulas are stored in a table, a data area, or an IFS file. The formula is only hard-coded in the sample program for the sake of illustration.

    • Any predefined variables a user might want to use will have to be available to the RPG program at calculation time.

    • Calculator’s result returns a generic DEC(15,5) value. This can be expanded to fit your needs if a greater precision or scale is required. On V5R3 systems, the maximum precision has been increased to 63! For extremely large or small numbers, create a version of Calculator that returns the double data type.

    • The SQL Prepare statement is expensive, in terms of performance, so don’t overuse it.

    • Any SQL column functions can be used in your expressions, such as random number generation, date calculations, trigonometric functions, and CASE expressions.

    • Any expression that returns a NULL will create an error. If you’re unhappy with this behavior, it can be avoided by always using the IFNULL function to force the result to zero, or by adding an indicator variable to the program.

    Sample program CALCR shows how easily these calculations can be done. Simply pass a column expression (conforming to DB2’s SQL syntax) as a parameter on the command line, and watch the program return the result. The program demo can accept two substitution variables QTY (replaced by a value of 5) and AMT (replaced by a value of 23.50).


    Note that the expression parameter is only 30 characters long.

    CALL CALCR PARM('3+3')    /* Displays 6 */
    
    CALL CALCR PARM('5*QTY')  /* Displays 25 */
    
    CALL CALCR PARM('ROUND(PI() * 2,2)')  /* Displays 6.28 */
    

    As you can see, SQL gives RPG the same power that the Eval function has given to Access, Visual Basic, VBScript, and JavaScript programmers: that is the ability to dynamically evaluate complex expressions without technical programming.

    Michael Sansoterra is a programmer/analyst for i3 Business Solutions, an IT services firm based in Grand Rapids, Michigan. Click here to contact Michael Sansoterra by e-mail.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Focal Point Solutions Group

    Comprehensive Data Protection from Focal Point SG

    Your organization needs to be thinking differently about your backup & disaster recovery strategy

    Concerns of the Industry

    • Inefficient manual backup processes
    • Effectively storing data offsite
    • Developing and testing a concrete disaster recovery plan
    • Efficient access to data in a disaster scenario for necessary users
    • Risk of cyber security attack
    • Declining IT staff and resources

    The true cause of the above concerns is an organization’s status quo – 80% of IBM i users currently backup to tape and 40% of companies have no DR plan at all. Don’t wait for a disaster to take action.

    The new way to ensure cost-effective safety

    • Automated cloud backup
    • Two (2) remote sites – redundant storage, power, internet pipe, firewalls, etc.
    • Data encryption at all times – in-flight and at-rest
    • Fully managed remote hardware DR, including remote VPN access for necessary users
    • Regularly simulated phishing tests and cyber security training

    Potential “landmines” in solutions to avoid

    • Single point of storage – no redundancy
    • Misleading data analysis, compression/de-dup ratios, sizing of necessary computer resources for backup and DR
    • Large-scale cloud storage with difficult recovery
    • Inability to meet RTO/RPO

    Don’t get caught like the many organizations we’ve seen with inefficient exposed backup data and no DR plan!

    What VAULT400 has to offer

    Backup

    • Native software agent schedules backups to the Focal Point SG cloud based on your retention scheme
    • Client data is backed up to two data centers in US or two data centers in Canada
    • 256-bit AES encryption in-flight and at rest – only the client has the encryption key
    • Detailed data analysis to ensure proper sizing

    Disaster Recovery as a Service (DRaaS)

    • Focal Point SG provides “hands-off” DR – fully managed recovery
    • 60 days of remote VPN access available to unlimited users in event of a disaster
    • Documented reports to ensure defined SLAs are met

    Managed Service Cyber Security Training

    • Fully managed phishing tests
    • Detailed reporting of results
    • Fully managed administration of custom online cyber security training

    VAULT400 Cloud Backup & DRaaS is an IBM Server Proven Solution.

    VAULT400.com/proposal for FREE analysis & proposal

    813.513.7402 | ContactUs@FocalPointSg.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Symantec Adds Regulatory Compliance to Security Management Tool How the i5s Compare with Other Big Boxes

    Leave a Reply Cancel reply

Volume 4, Number 38 -- November 10, 2004
THIS ISSUE
SPONSORED BY:

Linoma Software
ProData Computer Svcs
WorksRight Software

Table of Contents

  • Executing Dynamic Calculations with Embedded SQL
  • Let Me Out of Here!
  • More Conditional Sorting with SQL

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • IBM i Has a Future ‘If Kept Up To Date,’ IDC Says
  • When You Need Us, We Are Ready To Do Grunt Work
  • Generative AI: Coming to an ERP Near You
  • Four Hundred Monitor, March 22
  • IBM i PTF Guide, Volume 25, Number 12
  • Unattended IBM i Operations Continue Upward Climb
  • VS Code Is The Full Stack IDE For IBM i
  • Domino Runs on IBM i 7.5, But HCL Still Working on Power10
  • Four Hundred Monitor, March 6
  • IBM i PTF Guide, Volume 25, Number 11

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2023 IT Jungle