SAP SQLScript Development Guide
When to Use This Skill
Use this skill when writing SQLScript procedures, anonymous blocks, table/scalar functions, AMDP methods, exception handlers, cursor logic, bulk operations, or HANA performance-sensitive database logic that should run close to the data.
For browser-based Datasphere or HANA Cloud SQL editor triage, use sap-browser-automation for manual in-app authentication, consent-gated Edge profile reuse, fresh Edge/CDP startup, auth-state bootstrap, and recovery. Load local references/edge-cdp-control.md for SQLScript-specific boundaries. Use CDP only for local UI inspection, console diagnostics, deployment messages, and approved screenshots; default database validation still belongs in SQL/HANA tooling.
Overview
SQLScript is SAP HANA's procedural extension to SQL, enabling complex data-intensive logic execution directly within the database layer. It follows the code-to-data paradigm, pushing computation to where data resides rather than moving data to the application layer.
Key Characteristics
- Case-insensitive language
- All statements end with semicolons
- Variables use colon prefix when referenced (
:variableName) - No colon when assigning values
- Use
DUMMYtable for single-row operations
Two Logic Types
Table of Contents
- Overview
- Container Types
- Data Types
- Variable Declaration
- Control Structures
- Table Types
- Cursors
- Exception Handling
- AMDP Integration
- Performance Best Practices
- System Limits
- Debugging Tools
- Quick Reference
- Additional Resources
Container Types
1. Anonymous Blocks
Single-use logic not stored in the database. Useful for testing and ad-hoc execution.
Example:
2. Stored Procedures
Reusable database objects with input/output parameters.
3. User-Defined Functions
Scalar UDF - Returns single value:
Table UDF - Returns table (read-only):
Data Types
SQLScript supports comprehensive data types for different use cases. See references/data-types.md for complete documentation including:
- Numeric types (TINYINT, INTEGER, DECIMAL, etc.)
- Character types (VARCHAR, NVARCHAR, CLOB, etc.)
- Date/Time types (DATE, TIME, TIMESTAMP, SECONDDATE)
- Binary types (VARBINARY, BLOB)
- Type conversion functions (CAST, TO_ functions)
- NULL handling patterns
Variable Declaration
Scalar Variables
Note: Uninitialized variables default to NULL.
Table Variables
Implicit declaration:
Explicit declaration:
Using TABLE LIKE:
Arrays
Control Structures
IF-ELSE Statement
Comparison Operators:
Important: IF-ELSE cannot be used within SELECT statements. Use CASE WHEN instead.
WHILE Loop
FOR Loop
LOOP with EXIT
Table Types
Define reusable table structures:
Usage in procedures:
Cursors
Cursors handle result sets row by row. Pattern: Declare → Open → Fetch → Close
Performance Note: Cursors bypass the database optimizer and process rows sequentially. Use primarily with primary key-based queries. Prefer set-based operations when possible.
Complete Example:
FOR Loop Alternative:
Exception Handling
EXIT HANDLER
Suspends execution and performs cleanup when exceptions occur.
Condition values:
SQLEXCEPTION- Any SQL exceptionSQL_ERROR_CODE <number>- Specific error code
Access error details:
::SQL_ERROR_CODE- Numeric error code::SQL_ERROR_MESSAGE- Error message text
Example:
CONDITION
Associate user-defined names with error codes:
SIGNAL and RESIGNAL
Throw user-defined exceptions (codes 10000-19999):
Common Error Codes:
AMDP Integration
ABAP Managed Database Procedures allow SQLScript within ABAP classes.
Class Definition
Method Implementation
AMDP Restrictions
- Parameters must be pass-by-value (no RETURNING)
- Only scalar types, structures, internal tables allowed
- No nested tables or deep structures
- COMMIT/ROLLBACK not permitted
- Must use Eclipse ADT for development
- Auto-created on first invocation
Performance Best Practices
1. Reduce Data Volume Early
2. Prefer Declarative Over Imperative
3. Avoid Engine Mixing
- Don't mix Row Store and Column Store tables in same query
- Avoid Calculation Engine functions with pure SQL
- Use consistent storage types
4. Use UNION ALL Instead of UNION
5. Avoid Dynamic SQL
6. Position Imperative Logic Last
Place control structures at the end of procedures to maximize parallel processing of declarative statements.
System Limits
Note: Actual limits may vary by HANA version. Consult SAP documentation for version-specific limits.
Debugging Tools
- SQLScript Debugger - SAP Web IDE / Business Application Studio
- Plan Visualizer - Analyze execution plans
- Expensive Statement Trace - Identify bottlenecks
- SQL Analyzer - Query optimization recommendations
Quick Reference
String Concatenation
NULL Handling
Date Operations
Type Conversion
Related Skills
For comprehensive SAP development, combine this skill with:
Bundled Resources
Reference Documentation
references/skill-reference-guide.md- Index of all references with quick navigationreferences/glossary.md- SQLScript terminology and conceptsreferences/syntax-reference.md- Complete SQLScript syntax referencereferences/built-in-functions.md- Built-in functions catalogreferences/data-types.md- Data types and conversionreferences/exception-handling.md- Exception handling patternsreferences/amdp-integration.md- AMDP integration patternsreferences/performance-guide.md- Optimization techniquesreferences/advanced-features.md- Lateral joins, JSON, query hints, currency conversionreferences/troubleshooting.md- Common errors and solutionsreferences/edge-cdp-control.md- SQLScript-specific add-on for the sharedsap-browser-automationEdge/CDP and authentication layer
Production-Ready Templates
Copy and customize these templates for common patterns:
templates/simple-procedure.sql- Basic stored procedure with error handlingtemplates/procedure-with-error-handling.sql- Comprehensive error handling patternstemplates/table-function.sql- Table UDF with validationtemplates/scalar-function.sql- Scalar UDF examplestemplates/amdp-class.abap- Complete AMDP class boilerplatetemplates/amdp-procedure.sql- AMDP implementation templatetemplates/cursor-iteration.sql- Cursor patterns (classic and FOR loop)templates/bulk-operations.sql- High-performance bulk operations
Specialized Agents
- sqlscript-analyzer - Analyze code for performance issues and best practices
- procedure-generator - Generate procedures interactively from requirements
- amdp-helper - Assist with AMDP class creation and debugging
Slash Commands
/sqlscript-validate- Validate code with auto-fix capability/sqlscript-optimize- Performance analysis and optimization suggestions/sqlscript-convert- Convert between standalone and AMDP formats
Validation Hooks
Automatic code quality checks on Write/Edit operations:
- Error handling completeness
- Security vulnerabilities
- Performance anti-patterns
- Naming conventions
- AMDP compliance


