SapHanaTutorial.Com HOME     Learning-Materials Interview-Q&A Certifications Quiz Online-Courses Forum Jobs Trendz FAQs  
     Explore The World of Hana With Us     
About Us
Contact Us
 Apps
X
HANA App
>>>
Hadoop App
>>>
Tutorial App on SAP HANA
This app is an All-In-One package to provide everything to HANA Lovers.

It contains
1. Courses on SAP HANA - Basics, Modeling and Administration
2. Multiple Quizzes on Overview, Modelling, Architeture, and Administration
3. Most popular articles on SAP HANA
4. Series of Interview questions to brushup your HANA skills
Tutorial App on Hadoop
This app is an All-In-One package to provide everything to Hadoop Lovers.

It contains
1. Courses on Hadoop - Basics and Advanced
2. Multiple Quizzes on Basics, MapReduce and HDFS
3. Most popular articles on Hadoop
4. Series of Interview questions to brushup your skills
Apps
HANA App
Hadoop App
';
Search
Stay Connected
Search Topics
Topic Index
+
-
SQLScript
+
-
Tables in SAP HANA
+
-
Procedure
+
-
Examples of Procedure
Variable Scope Nesting in Procedure


In the previous article Local Scalar Variable , we learn about local scope variable in HANA.
In this article we will show how variables scope works in HANA.

Introduction:

SQLScript supports local variable declaration in a nested block. Local variables are only visible in the scope of the block in which they are defined. It is also possible to define local variables inside LOOP / WHILE /FOR / IF-ELSE control structures.

Create procedure:

Copy and paste the below script to create the procedure.
-- REPLACE <SCHEMA_NAME> WITH YOUR SCHEMA
CREATE PROCEDURE "<SCHEMA_NAME>"."VARIABLE_SCOPE_EXAMPLE"(
       OUT val INT)
        LANGUAGE SQLSCRIPT
        READS SQL DATA 
        AS
BEGIN
    DECLARE a INT := 1;
    BEGIN
        DECLARE a INT := 2;
        BEGIN
            DECLARE a INT;
            a := 3;
        END;
        val := a;
    END;
END;

Call procedure:

Call the procedure using below statement.

CALL  "<SCHEMA_NAME>"."VARIABLE_SCOPE_EXAMPLE"(?);

The output will be 2.

From this result you can see that the inner most nested block value of 3 has not been passed to the val variable.

Continue reading:
Check the next example on IF-ELSE Logic in Procedure




Support us by sharing this article.



Explore More
Close X
Close X

2 thoughts on “SAP HANA Procedure Example – Variable Scope Nesting in HANA

  1. harikrishna says:

    Hi,

    In the above example you have explained why the value 3 is not getting passed to variable a. Please let me know why the value is still 2 only.

    Regards
    Harikrishna Yoga

  2. harikrishna says:

    In the above example you have not explained why the value 3 is not getting passed to variable a. Please let me know why the value is still 2 only.

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *

 © 2017 : saphanatutorial.com, All rights reserved.  Privacy Policy