Sybase to Oracle Conversion

3
Sybase to Oracle differences T able below lists out the differences in the coding styles between Sybase and Oracle. There are subtle differences in syntax and major differences around Update statements with joins and error handling. T able 1 Comp arison of Stored procedure to load currencies if exists (select 1 from sysobjects where name = 'smpl_upd_ref_currency' and type = 'P') begin drop procedure smpl_upd_ref_currency end go create procedure smpl_upd_ref_currency as begin -- ------------------------------------------------- ---------------- -- PROCEDURE: smpl_upd_ref_curr ency -- AUTHOR: -- DATE: -- -- DESCRIPTION: Update FIN_CURRENCY with the reference data. -- -- REVISION: -- ------------------------------------------------- ---------------- -- ------------------------------------------------- ---------------- -- Declare and initialize variables. -- ------------------------------------------------- ---------------- declare @rc int declare @err int declare @err_type int declare @log_type int declare @msg varchar(80) declare @proc varchar(30) select @err_type = 1 select @log_type = 0 select @proc = 'smpl_upd_ref_currency' -- ------------------------------------------------- ---------------- -- 1. Update the currency data. -- ------------------------------------------------- ---------------- update fin_currency set crrncy_name = r.crrncy_name  from fin_currency, ref_currency r where fin_currency.crrncy_cd = r.crrncy_cd and fin_currency.crrncy_name != r.crrncy_name select @rc = @@rowcount, @err = @@error select @msg = "Update currency name in fin_currency table" exec smpl_log @proc, '01', @log_type, @msg, CREATE OR REPLACE PROCEDURE SMPL_UPD_REF_CURRENCY (o_ret_code OUT INTEGER) IS /* <HEADER> <PROCEDURE>smpl_upd_ref_currency</PROCEDURE> <AUTHOR></AUTHOR> <DATE></DATE> <DESCRIPTION> Update FIN_CURRENCY with the reference data. </DESCRIPTION> Modification History Date Revision By Modification -------- -------- ------- ------------------- <MODIFICATIONS> </MODIFICATIONS> </HEADER> */ k_proc CONSTANT VARCHAR2(30) := 'smpl_upd_ref_c urrency'; v_rc INTEGER; v_err INTEGER; v_msg VARCHAR2(80); v_section_id VARCHAR2(2); BEGIN  -- ----------------------------------------------------------- ---- -- 1. Update the currency Data. -- ----------------------------------------------------------- ---- v_section_id := '01'; v_msg := 'Updating currency name in fin_currency table'; UPDATE fin_currency c SET crrncy_name =  (SELECT r.crrncy_name FROM ref_currency r WHERE c.crrncy_cd = r.crrncy_cd AND c.crrncy_name != r.crrncy_name) WHERE EXISTS (SELECT * FROM ref_currency r2 WHERE c.crrncy_cd = r2.crrncy_cd AND c.crrncy_name != r2.crrncy_name);  v_rc := SQL%ROWCOUNT;  smpl_util_pkg.smpl_log_info(k_proc, v_section_id, v_msg, v_rc);  -- ----------------------------------------------------------- ---- 1 2 1 2 5 3 5 4 4 3

Transcript of Sybase to Oracle Conversion

Page 1: Sybase to Oracle Conversion

8/3/2019 Sybase to Oracle Conversion

http://slidepdf.com/reader/full/sybase-to-oracle-conversion 1/3

Sybase to Oracle differences

Table below lists out the differences in the coding styles between Sybase and Oracle. There are subtle

differences in syntax and major differences around Update statements with joins and error handling.

Table 1 Comparison of Stored procedure to load currencies

if exists (select 1from sysobjectswhere name = 'smpl_upd_ref_currency'and type = 'P')

begindrop procedure smpl_upd_ref_currency

endgo

create procedure smpl_upd_ref_currency

as

begin--

-----------------------------------------------------------------

-- PROCEDURE: smpl_upd_ref_currency-- AUTHOR:-- DATE:---- DESCRIPTION: Update FIN_CURRENCY with the

reference data.---- REVISION:--

-----------------------------------------------------------------

-------------------------------------------------------------------

-- Declare and initialize variables.--

-----------------------------------------------------------------

declare @rc intdeclare @err intdeclare @err_type intdeclare @log_type intdeclare @msg varchar(80)declare @proc varchar(30)

select @err_type = 1select @log_type = 0select @proc = 'smpl_upd_ref_currency'

-------------------------------------------------------------------

-- 1. Update the currency data.

-------------------------------------------------------------------

update fin_currencyset crrncy_name = r.crrncy_name

  from fin_currency, ref_currency rwhere fin_currency.crrncy_cd = r.crrncy_cdand fin_currency.crrncy_name != r.crrncy_name

select @rc = @@rowcount, @err = @@errorselect @msg = "Update currency name in

fin_currency table"exec smpl_log @proc, '01', @log_type, @msg,

CREATE OR REPLACE PROCEDURE SMPL_UPD_REF_CURRENCY(o_ret_code OUT INTEGER)

IS/*

<HEADER><PROCEDURE>smpl_upd_ref_currency</PROCEDURE>

<AUTHOR></AUTHOR><DATE></DATE>

<DESCRIPTION>Update FIN_CURRENCY with the reference data.</DESCRIPTION>

Modification HistoryDate Revision By Modification-------- -------- ------- -------------------<MODIFICATIONS></MODIFICATIONS>

</HEADER>

*/k_proc CONSTANT VARCHAR2(30) := 'smpl_upd_ref_currency';

v_rc INTEGER;v_err INTEGER;v_msg VARCHAR2(80);v_section_id VARCHAR2(2);

BEGIN

 --

--------------------------------------------------------------

-- 1. Update the currency Data.--

--------------------------------------------------------------

v_section_id := '01';v_msg := 'Updating currency name in fin_currency tabl

UPDATE fin_currency cSET crrncy_name =

  (SELECT r.crrncy_nameFROM ref_currency rWHERE c.crrncy_cd = r.crrncy_cd AND

c.crrncy_name != r.crrncy_name)WHERE EXISTS(SELECT * FROM ref_currency r2 WHERE c.crrncy_cd =

r2.crrncy_cd ANDc.crrncy_name != r2.crrncy_name);

 v_rc := SQL%ROWCOUNT;

 smpl_util_pkg.smpl_log_info(k_proc, v_section_id,

v_msg, v_rc); 

----------------------------------------------------------------

1

2

12

5

3

5

4

4

3

Page 2: Sybase to Oracle Conversion

8/3/2019 Sybase to Oracle Conversion

http://slidepdf.com/reader/full/sybase-to-oracle-conversion 2/3

@rc, @errif (@err != 0)begin

select @msg = "Error updating currencyname in fin_currency table"

exec smpl_log @proc, '01e', @err_type,@msg, @rc, @err

return -1end

---------------------------------------------------

------------------ 2. Insert any new currency data.--

-----------------------------------------------------------------

insert fin_currency (crrncy_cd,crrncy_name

)select r.crrncy_cd,

r.crrncy_name

from ref_currency r where not exists (select s.crrncy_cd

from fin_currency swhere r.crrncy_cd =

s.crrncy_cd)

select @rc = @@rowcount, @err = @@errorselect @msg = "Add new currencies to

fin_currency table"exec smpl_log @proc, '01', @log_type, @msg,

@rc, @err

if (@err != 0)begin

select @msg = "Error adding newcurrencies to fin_currency table"

exec smpl_log @proc, '01e', @err_type,@msg, @rc, @err

return -1end

-------------------------------------------------------------------

-- 99. Procedure end.--

-----------------------------------------------------------------

select @rc = 0, @err = @@errorselect @msg = "Procedure completed

successfully"

exec smpl_log @proc, '99', @log_type, @msg,@rc, @err

if (@err != 0)begin

select @msg = "Procedure completed witherror"

exec smpl_log @proc, '99e', @err_type,@msg, @rc, @err

return -1end

return 0

end

go

-- 2. Insert any new currency Data.--

--------------------------------------------------------------

v_section_id := '02';v_msg := 'Adding new currencies to the fin_currency

table';

INSERT INTO fin_Currency(crrncy_cd,crrncy_Name)

(SELECT r.crrncy_cd,

r.crrncy_NameFROM ref_currency r WHERE NOT EXISTS (SELECT s.crrncy_cd

FROM fin_Currency sWHERE r.crrncy_cd = s.crrncy_cd)

 v_rc := SQL%ROWCOUNT;smpl_util_pkg.smpl_log_info(k_proc, v_section_id,

v_msg, v_rc); 

----------------------------------------------------------------

-- 99. Procedure end.

----------------------------------------------------------------

v_section_id := '99';v_msg := 'Procedure completed successfully';smpl_util_pkg.smpl_log_info(k_proc, v_section_id,

v_msg, NULL); -- Row count doesn't mean anything at thispoint

o_ret_code := 0;RETURN;

EXCEPTIONWHEN OTHERS THEN

-- SV, 2/27, A generic Exception handler; This isto simply exception handling in these procedures;

-- Any SQL exception in this procedure will be

captured here; If any specific exceptions are to be-- handled in any section, add a BEGIN and ENDblock there with local exception handler(s).

-- v_section_id is used to identify which SQL(section) failed 

o_ret_code := 1;IF (v_section_id = '99') THEN

-- error was not due to update or insert; moregeneric error - this is rare, but just in case

o_ret_code := 99;v_msg := 'Procedure completed with error';

ELSEv_msg := 'Error ' || lower(v_msg);

END IF;smpl_util_pkg.smpl_log_error(k_proc, v_section_id

|| 'e', v_msg, SQLCODE, SQLERRM);

 -- RAISE;

END smpl_upd_ref_currency;

/

10

10

9

6

6

9

7

8

7

Page 3: Sybase to Oracle Conversion

8/3/2019 Sybase to Oracle Conversion

http://slidepdf.com/reader/full/sybase-to-oracle-conversion 3/3

Notes:

1. Sybase allows DDL statements inside stored procedures. Oracle does not allow such inside

PL/SQL. If the procedures need to be dropped it will have to be done using dynamic SQL.

Luckily Oracle provides a REPLACE directive that lets the procedure to be overwritten. This

will change the procedure if it already exists and retain the permissions too.

2. IS instead of AS

3. Header comments will now be written in XML. This is not an Oracle requirement, but a nice

have. We may be able to extract XML headers later to get formatted document.

4. Variables in Oracle are declared before BEGIN statement. Typical Oracle structure is

DECLARE BEGIN EXCEPTION END;

5. Oracle does not allow joins inside UPDATE or DELETE statements. A little convoluted sub-

query is required. Notice the same sub query is repeated in the WHERE clause with EXISTS

simulate the join. If EXISTS was left out, Oracle will try to update every row in the table bei

updated.

6. Error handling is different. Sybase allows status code of the procedure to be returned with a

RETURN <numeric value> statement. Oracle does not allow stored procedures to return any

values. In oracle, to mimic this, we could use the EXCEPTION handler. When the exception RAISEd inside the WHEN clause of the exception handler, the SQLCODE of the error is

returned back to calling procedure (or SQL*Plus). Another approach of using an OUT

 parameter for status value is considered as well.

7. Insert syntax is very similar except an INTO is required in Oracle.

8. smpl_log procedure in Sybase is replaced with a packaged procedure smpl_util_pkg.smpl_loThe reason for package is to isolate actual logging method used from calling procedures. And

also, with the package, we added some settings (variables) that will be available through thesession.

9. Statements that follow when the SQL statements executed successfully must be entered befor

the EXCEPTION block inside the current PL/SQL block. If the program always ran without errors, the exception block will never be reached.

10. Typically a ‘/’ (forward slash) is required in SQL*Plus to run what’s in buffer. Statem

terminator character (‘;’) is required to end statements in Oracle.