Lite runs SQL and PL/pgSQL routines; PL/SQL, T-SQL and DB2 SQL PL are planned. Functions and procedures declared with another LANGUAGE are rejected with “Unsupported function language”, and the Oracle-dialect examples on this page show the planned syntax.
Overview
The procedural language module enables:
Stored Procedures : Encapsulate complex business logic
User-Defined Functions : Create reusable computations
Control Flow : IF/ELSE, LOOP, WHILE, FOR statements
Variables : Declare, assign, and manipulate local variables
Exception Handling : EXCEPTION blocks for error handling
Cursors : Iterate over query results
Supported Dialects
Dialect Description Primary Use PL/pgSQL PostgreSQL procedural language Default, PostgreSQL compatibility T-SQL Microsoft SQL Server Planned PL/SQL Oracle Database Planned DB2 SQL PL IBM DB2 Planned Auto Auto-detect dialect Planned
Basic Syntax
Anonymous Blocks
SELECT COUNT ( * ) INTO v_count FROM users;
RAISE NOTICE ' Total users: % ' , v_count;
CREATE FUNCTION
CREATE OR REPLACE FUNCTION get_user_count ()
SELECT COUNT ( * ) INTO v_count FROM users;
CREATE PROCEDURE
CREATE OR REPLACE PROCEDURE update_user_status(
UPDATE users SET status = p_status WHERE id = p_user_id;
Variables
Declaration
v_name VARCHAR ( 100 ); -- Simple variable
v_count INTEGER : = 0 ; -- With default value
v_active CONSTANT BOOLEAN : = TRUE; -- Constant
v_user users%ROWTYPE; -- Row type
v_id users . id % TYPE ; -- Column type
Assignment
SELECT name INTO v_name FROM users WHERE id = 1 ;
SELECT name , email INTO v_name, v_email FROM users WHERE id = 1 ;
Control Flow
IF Statement
RAISE NOTICE ' High count ' ;
RAISE NOTICE ' Medium count ' ;
RAISE NOTICE ' Low count ' ;
CASE Statement
LOOP
-- FOR loop (numeric range)
RAISE NOTICE ' Iteration: % ' , i;
-- FOR loop (query result)
FOR rec IN SELECT * FROM users LOOP
RAISE NOTICE ' User: % ' , rec . name ;
Exception Handling
INSERT INTO users (id, name ) VALUES ( 1 , ' Alice ' );
WHEN unique_violation THEN
RAISE NOTICE ' User already exists ' ;
RAISE NOTICE ' Unexpected error: % ' , SQLERRM;
Common Exception Types
Exception Description unique_violationDuplicate key foreign_key_violationFK constraint failed null_value_not_allowedNULL in NOT NULL column check_violationCHECK constraint failed division_by_zeroDivision by zero OTHERSCatch-all handler
Cursors
user_cursor CURSOR FOR SELECT * FROM users;
user_record users%ROWTYPE;
FETCH user_cursor INTO user_record;
RAISE NOTICE ' User: % ' , user_record . name ;
Dialect-Specific Examples
Planned
HeliosDB Lite runs LANGUAGE sql and LANGUAGE plpgsql routines. T-SQL, PL/SQL
and DB2 SQL PL routines are planned; the examples below show the planned syntax.
Oracle-style scalar functions such as NVL, SYSDATE and TO_CHAR are
available today.
T-SQL Style
-- T-SQL uses @ for variables
SET @v_count = @v_count + 1 ;
PL/SQL Style
-- PL/SQL uses := for assignment
DBMS_OUTPUT . PUT_LINE (v_count);
Integration with Triggers
Procedural language functions are commonly used as trigger handlers:
-- Create trigger function
CREATE OR REPLACE FUNCTION audit_changes ()
INSERT INTO audit_log (table_name, action , changed_at)
VALUES (TG_TABLE_NAME, TG_OP, NOW ());
CREATE TRIGGER users_audit
AFTER INSERT OR UPDATE OR DELETE ON users
EXECUTE FUNCTION audit_changes();
Built-in Functions
Utility Functions
Function Description RAISE NOTICEOutput debug message RAISE EXCEPTIONThrow error FOUNDBoolean - last query returned rows ROW_COUNTRows affected by last statement NOW()Current timestamp
Trigger Context Variables
Variable Description TG_OPOperation (INSERT, UPDATE, DELETE) TG_TABLE_NAMEName of trigger table OLDRow before modification NEWRow after modification TG_WHENBEFORE or AFTER
PL functions are compiled and cached
Avoid excessive dynamic SQL
Use bulk operations where possible
Consider using IMMUTABLE/STABLE/VOLATILE appropriately
See Also