sybase to oracle_conversion

3

Click here to load reader

Upload: sam-varadarajan

Post on 11-Jun-2015

1.706 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Sybase to oracle_conversion

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 currenciesif exists (select 1 from sysobjects where name = 'smpl_upd_ref_currency' and type = 'P')begin drop procedure smpl_upd_ref_currencyendgo

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 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 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 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

12

5

3

5

4

4

3

Page 2: Sybase to oracle_conversion

@rc, @err if (@err != 0) begin select @msg = "Error updating currency name in fin_currency table" exec smpl_log @proc, '01e', @err_type, @msg, @rc, @err return -1 end

-- ----------------------------------------------------------------- -- 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 s where r.crrncy_cd =

s.crrncy_cd)

select @rc = @@rowcount, @err = @@error select @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 new currencies to fin_currency table" exec smpl_log @proc, '01e', @err_type, @msg, @rc, @err return -1 end

-- ----------------------------------------------------------------- -- 99. Procedure end. -- ----------------------------------------------------------------- select @rc = 0, @err = @@error select @msg = "Procedure completed successfully"

exec smpl_log @proc, '99', @log_type, @msg, @rc, @err if (@err != 0) begin select @msg = "Procedure completed with error" exec smpl_log @proc, '99e', @err_type, @msg, @rc, @err return -1 end

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_Name

FROM ref_currency r WHERE NOT EXISTS (SELECT s.crrncy_cd FROM fin_Currency s WHERE 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 this point

o_ret_code := 0; RETURN;

EXCEPTION WHEN OTHERS THEN -- SV, 2/27, A generic Exception handler; This is to 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 END block 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; more generic error - this is rare, but just in case o_ret_code := 99; v_msg := 'Procedure completed with error'; ELSE v_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

8

6

6

9

7

8

7

Page 3: Sybase to oracle_conversion

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 to 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 to simulate the join. If EXISTS was left out, Oracle will try to update every row in the table being 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 is 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_log. The 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 the session.

9. Statements that follow when the SQL statements executed successfully must be entered before the EXCEPTION block inside the current PL/SQL block. If the program always ran without any errors, the exception block will never be reached.

10. Typically a ‘/’ (forward slash) is required in SQL*Plus to run what’s in buffer. Statement terminator character (‘;’) is required to end statements in Oracle.