Complex Numeric Type Conversion
Table of Contents
- 1. Key SQL Logic in Source Oracle Database
- 2. SQL Refactoring Plan for Target MySQL
- 3. Refactoring Summary
1. Key SQL Logic in Source Oracle Database
CREATE OR REPLACE PROCEDURE process_transaction AS
v_balance NUMBER(10,2);
v_transfer NUMBER(10,2) := 10000.00;
BEGIN
SELECT account_balance
INTO v_balance
FROM user_accounts
WHERE account_id = 12345;
v_balance := v_balance + TO_NUMBER('2000.00');
UPDATE user_accounts
SET account_balance = v_balance - v_transfer
WHERE account_id = 12345;
COMMIT;
END process_transaction;
2. SQL Refactoring Plan for Target MySQL
CREATE PROCEDURE process_transaction()
BEGIN
DECLARE v_balance DECIMAL(10,2);
DECLARE v_transfer DECIMAL(10,2) DEFAULT 10000.00;
-- Retrieve account balance (row-level lock applied)
SELECT account_balance
INTO v_balance
FROM user_accounts
WHERE account_id = 12345
FOR UPDATE;
SET v_balance = v_balance + 2000.00;
UPDATE user_accounts
SET account_balance = v_balance - v_transfer
WHERE account_id = 12345;
COMMIT;
END;
3. Refactoring Summary
- Stored procedure structure refactoring:
- Replace
CREATE OR REPLACE PROCEDURE ... ASwithCREATE PROCEDURE(). - Move variable declarations from the
ASblock to explicitDECLAREstatements. - Add
FOR UPDATElocking to improve transactional consistency (MySQL default isolation isREPEATABLE READ).
- Replace
- Accurate numeric type mapping:
- Map
NUMBER(10,2)toDECIMAL(10,2)to preserve precision and range. - Map
BINARY_FLOATtoFLOATfor floating-point operations. - Replace
TO_NUMBER()with direct numeric operations to avoid implicit-conversion risks.
- Map
- Variable initialization optimization: replace Oracle
:=assignments with declarativeDEFAULTinitialization where applicable.
Image credit: Designed by Freepik