SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Use Table Variable Operators

Leverage SQLScript in stored procedures, user defined functions, and user defined libraries.

Overview

🎓 intermediate 10 min. SAP HANAIntermediateSqlSAP HANA CloudSAP Business Application Studio

You will learn

  • โœ”How to update procedure to use Table Variable Operators to manipulate the immediate table variables
Rich Heilman R Rich Heilman November 1, 2022
Created by February 11, 2021
Contributors

Prerequisites

Prerequisites

Steps

Intro

In this tutorial, you will update the previous procedure to now use Table Variable Operators to manipulate the immediate table variables. We can use table variable operators to perform DML like operations on table variables without having to invoke the SQL layer. In this exercise, we will experiment with the INSERT, UPDATE, DELETE, and SEARCH operators.


Step 1 Use INSERT operator
โ€”

  1. Return to the procedure called build_products in the procedure folder.

    procedure editor
    procedure editor

  2. Remove the code inside the body, between the BEGIN and END statements leaving only the two lines shown here.

    remove
    remove

  3. After the SELECT statement, Use the INSERT operator to copy all rows of lt_products into the output parameter called ex_products. Then use the INSERT operator to insert 3 new products into the output table. Since you are specifying the actual index, it will insert your new rows at that index and push all existing rows down.

SQLScript
:ex_products.INSERT(:lt_products);
:ex_products.INSERT(('ProductA', 'Software', '1999.99'), 1);
:ex_products.INSERT(('ProductB', 'Software', '2999.99'), 2);
:ex_products.INSERT(('ProductC', 'Software', '3999.99'), 3);
  1. The complete code should look very similar to this.

    SQLScript
    PROCEDURE "build_products" (
    		        out ex_products table (PRODUCTID nvarchar(10),
    	                               CATEGORY nvarchar(20),
    	                               PRICE decimal(15,2) ) )
    	   LANGUAGE SQLSCRIPT
    	   SQL SECURITY INVOKER
    	   READS SQL DATA AS
    BEGIN
    
     declare lt_products table like :ex_products;
    
    	 lt_products = select PRODUCTID, CATEGORY, PRICE from "OPENSAP_MD_PRODUCTS";
    	 :ex_products.INSERT(:lt_products);
    	 :ex_products.INSERT(('ProductA', 'Software', '1999.99'), 1);
    	 :ex_products.INSERT(('ProductB', 'Software', '2999.99'), 2);
    	 :ex_products.INSERT(('ProductC', 'Software', '3999.99'), 3);
    
    END
  2. Use what you have learned and Save your work, and perform a Deploy. Then return to the Database Explorer and call the procedure. In the Results tab, you should see the product data including the new products that you have inserted.

    results
    results

Step 2 Use UPDATE operator
+
Step 3 Use DELETE operator
+
Step 4 Use SEARCH operator
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 4
1. Use INSERT operator 2. Use UPDATE operator 3. Use DELETE operator 4. Use SEARCH operator

Learn more →