Stored Procedure in SAP HANA (SQL Tutorial)
โก Smart Summary
SAP HANA SQL Stored Procedure is a reusable, named block of SQLScript that performs a specific data operation. It accepts IN, OUT, and INOUT parameters, can be marked read-only with READS SQL DATA, and is created at schema or package level using the CREATE PROCEDURE syntax.

What is an SQL Stored Procedure?
A stored procedure is a named, reusable unit of code that performs a specific task. Procedures can be composed into larger programs, which is the foundation of modular design. One procedure can call another โ the caller is referred to as the calling program.
Procedures are reusable processing blocks with a defined sequence of data transformations. They can accept multiple input and output parameters and can be declared read-only or read-write.
An SQL procedure in SAP HANA can be created at:
- Schema level (Catalog node) โ managed through the catalog and visible to every consumer with the right privileges.
- Package level (Content node) โ managed as a design-time artefact inside an application package.
SAP HANA Stored Procedure Syntax
The general syntax for creating a procedure in SAP HANA is shown below.
CREATE PROCEDURE <proc_name> [(<parameter_clause>)] [LANGUAGE <lang>]
[SQL SECURITY <mode>] [DEFAULT SCHEMA <default_schema_name>]
[READS SQL DATA [WITH RESULT VIEW <view_name>]] AS
{ BEGIN [SEQUENTIAL EXECUTION]
<procedure_body>
END
| HEADER ONLY }
The CREATE PROCEDURE statement creates a procedure using the programming language specified by <lang>. The default language is SQLScript.
Syntax Elements Explained
The table below describes every clause used in the CREATE PROCEDURE syntax.
| Element | Description |
|---|---|
<proc_name> |
Name of the procedure. |
<parameter_clause> |
List of parameters, each marked IN, OUT, or INOUT: โข IN โ read-only input passed to the procedure. โข OUT โ output returned from the procedure. โข INOUT โ both passes a value in and returns the modified value out. |
LANGUAGE <lang> |
Programming language used in the procedure. Default: SQLSCRIPT. |
SQL SECURITY <mode> |
Security mode (Default: DEFINER): โข DEFINER โ runs with the privileges of the procedure’s creator. โข INVOKER โ runs with the privileges of the caller. |
DEFAULT SCHEMA <default_schema_name> |
Schema used to resolve unqualified objects in the procedure body. If omitted, the current session schema is used. |
READS SQL DATA |
Marks the procedure as read-only. It cannot modify data or schema, contains no DDL or DML, and can only call other read-only procedures. |
WITH RESULT VIEW <view_name> |
Defines the result view used as the procedure’s output. When a result view is present, the procedure can be queried by another SQL statement as if it were a table or a view. |
SEQUENTIAL EXECUTION |
Forces the procedure body to execute sequentially with no parallelism. |
<procedure_body> |
Main body of the procedure, written in the selected programming language. |
HEADER ONLY |
Creates only the procedure’s properties with an OID, without compiling the body. Useful for forward declarations. |
Example: Create a Simple Stored Procedure
The example below creates a read-only procedure that returns sales total for a given customer.
CREATE PROCEDURE GET_CUSTOMER_SALES (
IN iv_customer_id NVARCHAR(10),
OUT ev_total DECIMAL(15,2)
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
READS SQL DATA AS
BEGIN
SELECT SUM(NET_VALUE) INTO ev_total
FROM "SALES"."ORDERS"
WHERE CUSTOMER_ID = :iv_customer_id;
END;
Call the procedure with:
CALL GET_CUSTOMER_SALES ('C0001', ?);
SAP HANA Studio (or any JDBC/ODBC client) returns the value of ev_total in the output parameter.
Calling a Stored Procedure
Once created, a stored procedure is invoked through the CALL statement, optionally inside another procedure, an SQLScript function, or an external client. The four most common invocation patterns are:
- From SQL:
CALL my_proc(p1, p2, ?);โ placeholders return OUT values. - From another procedure:
CALL another_proc(:in1, :out1); - From an application: bind input parameters and read output through JDBC, ODBC, or the Cloud SDK.
- As a virtual table: when a result view is declared, query the procedure with a SELECT against the view name.
Best Practices for HANA Stored Procedures
The habits below keep HANA procedures readable, performant, and easy to maintain:
- Name parameters with prefixes:
iv_for input,ev_for export, andiov_for input/output. - Mark intent with READS SQL DATA wherever the procedure is genuinely read-only.
- Use SQL SECURITY INVOKER for shared procedures that should respect the caller’s privileges.
- Wrap each block in BEGIN/END and keep the body short โ split heavy logic into helper procedures.
- Handle exceptions: use
DECLARE EXIT HANDLERfor graceful error reporting. - Avoid SELECT * inside procedures โ name the columns you actually need so plans stay stable.
