Do what you love, and you’ll never work another day in your life. I strongly believe in this maxim and always strive to find interesting stuff at my work place. Yes, I work in the blue screen of SAP whole day and I can proudly say it is not boring at all. Though many complain, but I love what I do. Did you know we can secretly chat using SAP ABAP? No, no.. you do not need to build another chatting tool. There is a standard function module ‘ TH_POPUP ‘ which you can use straight out of the box. Check my other article ABAP – Power to Kill to learn more about the SAP ABAP chat.
The point I am trying to make is, there are tons and tons of interesting activities we can perform in SAP. Do not say, you are too busy with your project work and you cannot explore. I would suggest, cut down 30 minutes from your Facebook browsing, 1 hour from going through the fake news and unnecessary messages in Whatsapp and 30 minutes from Instagram. You now have 2 hours of time to explore something new in SAP.
Check how Stephan created a Simple Calculator in SAP ABAP.
With the same philosophy to try something new and explore more in SAP, we are going to learn something new and different in ABAP programming today.
Scenario:
Today morning, one of the functional consultants came to me and said that our business team is becoming crazy day by day. (well…we ABAPer says the same things for functional consultants too).. shhhhh
After his statement, he revealed the requirement of business team..
Problem statement:
There should be a text box in the selection screen, in which user can put any arithmetic operations, conditional statements, string operations etc and after its execution the result should get printed. In short, they wanted to write some ABAP Commands (open SQL) in the text box and it should give the desired output.
Resolution Discussion:
After hearing this, we both laughed on the business requirement and their craziness. We thought it was a joke. But the business team members were serious. They wanted us to develop something and provide the solution.
He rested his case that day and left me pondering. I like crazy ideas. But not that crazy.
Now the ball was in my court. How to provide a solution which will be dynamic and scalable? They want to write any queries in the text box and expect an output.
After all R&D, I narrowed down the below solution which treats the selection text box as a mini ABAP editor. I felt like a genious inventor after this solution. Yes, I need to pat my back for this innovative solution.
Here user can put any ABAP query with any arithmetic operations, IF conditions, Select queries, Loops, Read statements and what not. And the beauty is, it gets executed and provides the result in output. But the syntax has to be correct when you put the query in the text box.
Check my other Article – A to Z about AL11
Excited right??…. Let’s check it out.
Resolution:
There are many ways in ABAP to do dynamic coding; like this field-symbols or dynamic internal table creation etc.
From this list, we are going to use the Subroutine Pool logic.
Let’s see, what’s the subroutine pool is all about..
Subroutine Pool:
It contains collections of subroutines that can be called externally from other programs.
Syntax of subroutine pool:
GENERATE SUBROUTINE POOL <itab> NAME <prog> [<options>].
Details:
The above statement generates a temporary subroutine pool in the main memory area of the running program.
<itab>: The code of the subroutine pool is written in internal table which is passed under <itab>.
<prog>: The generated subroutine pool is stored internally under program name <prog>.
<options>: Below options are there to pass our subroutine:
Options | Description |
MESSAGE <mess> | In the case of a syntax error, field <mess> contains the error message. |
INCLUDE <incl> | In the case of a syntax error, field <incl> contains the name of the include program in which the error possibly occurred. |
LINE <line> | In the case of a syntax error, field <line> contains the number of the wrong line. |
WORD <word> | In the case of a syntax error, field <word> contains the wrong word. |
OFFSET <offs> | In the case of a syntax error, field <offs> contains the offset of the wrong word in the line. |
TRACE-FILE <trac> | If you use this option, you switch on the trace mode and field <trac> contains the trace output. |
So, the syntax which will be there in our program is:
<code>GENERATE SUBROUTINE POOL src NAME prog_name MESSAGE msg LINE line WORD word OFFSET off.</code>
Also Watch – Free Video Course on New ABAP 7.4 Features
Let’s dive deep..
Program explanation:
We need to create a mother program which will take input query from the User and displays its output.
As we will create a temporary program through GENERATE SUBROUTINE option, we need to write the name and declarations for that temporary program:

In above screenshot (red box), I have provided the name as ZSB_SUBPOOL to my temporary program and stored all declaration in SRC with is of table type string. You know what SB stands for in the 2nd and 3rd character of the program name?
Please note: You can give any name without Z or Y. Try giving the name as SUBPOOL and it will still work.
Next, create a subroutine named as ‘DYN1’ whose PERFORM will be called after GENERATE SUBROUTINE statement also passing the input parameter string P_FORM to SRC in that form.
Then we are appending parameter p_form to src.
At the end writing the RESULT on the screen with Write statement.
And then end the subroutine with ENDFORM.
<code>FORM DYN1 USING p_form TYPE string. APPEND p_form TO src. WRITE / RESULT. ENDFORM.</code>
The final step is to call this temporary program via GENERATE SUBROUTINE POOL method.
This is a very powerful command. It does the syntax check and if everything is ok, it creates the temporary program in memory and executes it.
Once the Generate Subroutine Pool step is over and there is no error, it will call the program with logic written in PERFORM ‘DYN1’.


If you input the above text in the selection screen and debug the code, you will get to see the whole code of temporary program as below:

Isn’t it cool?
Code Snippet:
<code>REPORT zdynamic_query_sb. TYPES: source_line(256) TYPE c. DATA: src TYPE TABLE OF source_line, prog_name(30) TYPE c, msg(120) TYPE c, line(10) TYPE c, word(10) TYPE c, off(3) TYPE c. APPEND 'PROGRAM SUBPOOL.' TO src. APPEND 'DATA RESULT TYPE string.' TO src. APPEND 'DATA: a TYPE DECFLOAT34 VALUE ''5.0''.' TO src. APPEND 'DATA: b TYPE DECFLOAT34 VALUE ''4.5''.' TO src. PARAMETERS p_form TYPE string. APPEND 'FORM DYN1 USING p_form TYPE string.' TO src. APPEND p_form TO src. APPEND ' WRITE / RESULT.' TO src. APPEND 'ENDFORM.' TO src. GENERATE SUBROUTINE POOL src NAME prog_name MESSAGE msg LINE line WORD word OFFSET off. IF sy-subrc <> 0. WRITE: / 'Error during generation in line', line, / msg, / 'Word:', word, 'at offset', off. ELSE. PERFORM dyn1 IN PROGRAM (prog_name) USING p_form. ENDIF.</code>
Time to Test
Execute the program and we will get below selection screen:

Insert any sample ABAP query to get its result.
Please Note: RESULT should always be the variable name for our case which you want to print.

After execution, we will get result of above query as below:

APPEND 'DATA: a TYPE DECFLOAT34 VALUE ''5.0''.' TO src. APPEND 'DATA: b TYPE DECFLOAT34 VALUE ''4.5''.' TO src.
Also, you can access the global variable which you defined in the memory program. Here we defined A and B variables in the the program. We can provide them on selection screen text field and can access them in memory program as well.

The value of variable A is ‘5.0’ whereas the value of B is ‘4.5’ so the RESULT of above query will be ‘0.5’.

Some random Quiz questions:
- What would be the output of this text input?
RESULT = ‘HELLO SAPYARD’. in the text field? - Do you this this input will work?
data(ls_1) = ‘Swapna’. data(ls_2) = ‘Dream’. Concatenate ls_1 ls_2 into RESULT SEPARATED BY SPACE. - What would be the output for this input text?
data(ln_1) = 10. data(ln_2) = 5. RESULT = ln_1 * ln_2
Solution




In this program you can write any ABAP query like from arithmetic operations, conditional operations or even select query too. It Magic. Right?
So what are you waiting for? Copy the program and enjoy this mini ABAP query processor which you just created.
SAP is fun. We just need to find out how to have fun!!
Comments Please.
5300+ SAP Technical Practitioners from 6 Continents of the SAP World. Install Telegram app and click this link to join – Telegram SAP Technical Discuss Group.
Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.
Check HANA-ABAP Tutorials
- ABAP on SAP HANA. Part I. First Program in ABAP HANA
- ABAP on SAP HANA. Part II. ADT Eclipse and HANA Studio
- ABAP on SAP HANA. Part III. Debugging in ADT
- CDS Part 1. Core Data Services – Introduction
- CDS Part 2. Core Data Services – Deep Dive
- ABAP on SAP HANA. Part VI. New Age Open SQL ABAP 740
- ABAP on SAP HANA. Part VII. SQL Script and SAP HANA Stored Procedure
- ABAP on SAP HANA. Part VIII. ADBC – ABAP DataBase Connectivity
- ABAP on SAP HANA. Part IX. AMDP – ABAP Managed Database Procedure
- ABAP on SAP HANA. Part X. AMDP with SELECT OPTIONS
- ABAP on SAP HANA. Part XI. Are Native SQL and Open SQL Competitors?
- ABAP on SAP HANA. Part XII. Open SQL, CDS or AMDP, which Code to Data Technique to use?
- ABAP on SAP HANA. Part XIII. Sample Functional Specification of HANA Project
- ABAP on SAP HANA: Part XIV. HANA Ready, HANA-tization & HANA Plus
- CDS Part 3. Expose CDS Views as OData Service through Annotation
- ABAP on SAP HANA: Part XVI. HANAtization
- ABAP on SAP HANA: Part XVII. ATC – ABAP Test Cockpit Setup & Exemption Process
- SAP ABAP on HANA: Part XVIII. SALV IDA (Integrated Data Access)
- ABAP for SAP HANA. Part XIX. Sample Technical Specification of HANA Project
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- 4 Useful Tips on ABAP and ABAP on HANA
- Associations in HANA – A Conceptual Approach
- CDS Part 5. ABAP CDS Views With Authorization
- ABAP for SAP HANA. Part XXII. How to Consume Native HANA Views Using ADBC?
- CDS Part 6. Basic Expressions & Operations Available for CDS View – I
- 115 ABAP for SAP HANA Interview Questions & Answers
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- CDS Part 8. Usage of Built-In Functions in CDS – I
- CDS Part 9. Usage of Built-In Functions in CDS – II
- CDS Part 10. Usage of Built-In Functions in CDS – III
- CDS Part 11. How to Consume CDS View in Smart Business Service KPI Fiori Apps?
- CDS Part 12. Useful 6 CDS Related Tools in ADT
- CDS Part 13. Key Definition in CDS Views
- CDS Part 14. ABAP Annotations for Translatable Texts in CDS Views
- CDS Part 15. Associations in CDS Views – I
- ABAP for SAP HANA – Part 23. How to Access Database Schema Dynamically Using AMDP?
- Video Course – New Features and Syntaxes in SAP ABAP 7.40+ with Exercises
- CDS Part 16. Usage of Built-In Functions in CDS – IV
- ‘ABAP for SAP HANA’ Points to Remember
- Limitations of Open SQL Compared to SQL Script in HANA
- CDS Part 17. How to Overcome GUID Mismatch Linking Problem in ABAP CDS?
- Curious Case of ATC – Priority 2 – Low Performance Operations on Internal Tables
- ABAP for SAP HANA – Part 24. Custom Report Using AMDP