Home > Documentation > Oracle to PostgreSQL
Convert Oracle Stored Procedures to PostgreSQL |
[Cursors] [Handling errors] |
This whitepaper explores differences between procedure languages of Oracle and PostgreSQL. Although these languages are basically similar, there are few important differences to remember during migration from one DBMS to another:
Let us illustrate porting functions from Oracle to PostgreSQL on few examples, starting from simple function concatenating two strings. In Oracle:
CREATE OR REPLACE FUNCTION my_concat(str1 varchar, str2 varchar) RETURN varchar IS BEGIN IF str2 IS NULL THEN RETURN str1; END IF; RETURN str1 || ' ' || str2; END; / show errors;
And the PostgreSQL equivalent is:
CREATE OR REPLACE FUNCTION my_concat(str1 varchar, str2 varchar) RETURNS varchar AS $$ BEGIN IF str2 IS NULL THEN RETURN str1; END IF; RETURN str1 || ' ' || str2; END; $$ LANGUAGE plpgsql;
Next example is more complicated, this is function extracting data from table and processing it before returning via OUT-parameter:
CREATE OR REPLACE PROCEDURE get_balance( v_ID IN VARCHAR, v_balance OUT NUMBER) BEGIN SELECT SUM( DECODE( BALANCE_SIGN, '+', BALANCE_AMOUNT, -1 * BALANCE_AMOUNT)) INTO v_balance FROM ACCOUNTING WHERE ID LIKE v_ID AND months_between(CURRENT_TIMESTAMP, p_date) < 2 GROUP BY ID; END; / show errors;
Porting this code to PostgreSQL requires implementation of intermediate function for Oracle 'months_between':
CREATE OR REPLACE FUNCTION months_of(interval) RETURNS int AS $$ SELECT EXTRACT(years from $1)::int * 12 + EXTRACT(years from $1)::int; $$ LANGUAGE sql;
And PostgreSQL equivalent of the original function is:
CREATE OR REPLACE FUNCTION get_balance( v_ID IN VARCHAR, v_balance OUT NUMERIC) AS $$ BEGIN SELECT SUM( (CASE WHEN BALANCE_SIGN='+' THEN BALANCE_AMOUNT ELSE -1 * BALANCE_AMOUNT END) INTO v_balance FROM ACCOUNTING WHERE ID LIKE v_ID AND abs(months_of(age(CURRENT_TIMESTAMP, p_date))) < 2 GROUP BY ID; END; $$ LANGUAGE plpgsql;
Specialists of Intelligent Converters are always ready to assist in migration of stored procedures, triggers and other database entries. See Database Migration Service for details.