ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

exsr with variable sr to call

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • exsr with variable sr to call

    Hello everyone,

    I have to ask a (maybe) stupid thing, but I never did it and I don't know if it's possible to do. I have to read a field value and execute a subroutine whose name is the field value.

    Example:

    select
    when myField = 'subABC'
    exsr subABC
    when myField = 'subQWERTY'
    exsr subQWERTY

    ... etc ...

    endsl


    Is there a way to write something like this?
    exsr myFieldValue

    Thank you so much!!

  • #2
    I don't think you can with subroutines. But I think you can with subprocedures: https://www.itjungle.com/2010/03/31/fhg033110-story01/

    Comment


    • JonBoy
      JonBoy commented
      Editing a comment
      Yup. And in fact there is a way to use APIs to make the thing dynamic - but given that the OP was looking at calling subroutines then this is a valid and pretty easy way to do it. In addition to the article you listed I also did a couple of other articles for IBM Systems mag that cover a similar approach but is completely data driven.

      These are the links: https://authory.com/JonParisAndSusan...r-Calculations
      and: https://authory.com/JonParisAndSusan...e-Calculations

      Sorry about the source formatting. We've been able to save the articles from extinction but have not yet had time to do all the formatting.

  • #3
    Thank you so much for these really interesting answers, I will study them also to solve other kind of problems. Unfortunately the first thing I can understand is that my question wasn't so stupid as I hoped.... The goal "simply" was to use a variable instead of a constant in EXSR operation.

    Comment


    • #4
      Basically you can not usually do what you were asking for in a compiled language. Scripting languages like Python, PHP, node.js, etc. usually have an ability to do this kind of thing but not compiled languages like C, C++, COBOL, RPG, etc.

      If you have any questions please feel free to holler - I've written a number of things using techniques like these.

      Comment


      • #5
        There is some kind of interpreter language in RPG, isn't it?
        I'm thinking of prepared cursor SQL.

        Code:
        arg1 = '7';
        op = '+';
        arg2 = '5';
        sqlcmd = 'values( select ' + arg1 + ' ' + op + ' ' + arg2 + ' from sysibm.sysdummy1 ) into ?';
        exec sql prepare sqlstatement from :sqlcmd;
        exec sql execute sqlstatement using :result;
        // result is '12' now
        Of course you have to be careful to prevent SQL injection or damages by unexpected values, so the program should first check the fields that are used to build the sql command.

        Comment


        • #6
          Well SQL is not an "interpreter" - but yes you can imbed SQL in RPG. I don't think you can use a parameter marker in this way though. You would simply name the target INTO structure with a colon preceding the name.

          This looks a bit like using a steamroller to open a walnut though. The technique I outlined in the "Flexible" articles referenced previously offers a more practical approach.

          We could offer far more sensible solutions if we knew _exactly_ what you were trying to achieve and why. You originally proposed a solution (Subroutines) which couldn't work - but subprocedures could do what you asked. But now you've leapt to SQL and we still don't really know what you are trying to do.

          Comment


          • #7
            Ok, thank you for clarification. What I meant by "some kind of" (I put it this way because I don't know how it works in detail under the hood) is that SQL works like an interpreted language in regards to producing dynamic code that can be built at runtime.

            You can write prepared sql like this. I used it before and I tested the code I quoted. :-)
            https://code400.com/forum/forum/iser...ursor-possible
            Last edited by Scholli2000; August 25, 2020, 12:45 AM.

            Comment


            • #8
              Yes I'm well aware of that.

              But you still haven't told us what you're trying to do.

              Comment


              • #9
                Sorry, so I had misunderstood your answer (as you have probably already guessed, English is not my native language). I'm a big fan of you and Susan and I owe you a lot and maybe I thought I could make up for it a little bit by mentioning the parameter markers notation. I really didn't want to question your competence.

                Maybe hook400 can tell us what he actually wanted to do.

                Comment


                • JonBoy
                  JonBoy commented
                  Editing a comment
                  My apologies - I was in a hurry when responding and thought you _were_ hooK400! I was a little sharp in my response because of that. Must learn to read who the post is from!
              Working...
              X