Solved

Error Handling On Insert Statements - Converting SQL Server Syntax to Oracle

Posted on 2014-01-21
2
548 Views
Last Modified: 2014-01-22
This is probably a pretty easy question, but I'm not an Oracle expert and I'm having trouble nailing down the Syntax.

I need to replicate this in Oracle, but thus far I can only seem to get it to work inside a stored procedure.


INSERT BATCH_JOB_INSTANCE ([JOB_NAME],[JOB_KEY],[JOB_INSTANCE_ID],[VERSION])
SELECT 'aa','bb',4183,1876149191
GO
IF @@error <> 0
PRINT 'There was an error inserting data into table BATCH_JOB_INSTANCE, see error message above.'
GO

INSERT BATCH_JOB_INSTANCE ([JOB_NAME],[JOB_KEY],[JOB_INSTANCE_ID],[VERSION])
SELECT 'cc',dd',332,3423
GO
IF @@error <> 0
PRINT 'There was an error inserting data into table BATCH_JOB_INSTANCE, see error message above.'
GO
0
Comment
Question by:Jay Redd
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 39799035
Setup a base table and insert statement:

SQL> create table t1 (x1 char(10), x2 number);

Table created.

SQL> insert into t1 values ('x1','x2');
insert into t1 values ('x1','x2')
                            *
ERROR at line 1:
ORA-01722: invalid number

Open in new window


Write a PL/SQL block to trap an insert statement fail:

SQL> set serveroutput on
SQL> declare
  2     v_code NUMBER;
  3     v_errm VARCHAR2(64);
  4  begin
  5     insert into t1 values ('x1','x2');
  6  exception
  7     when others then
  8           v_code := SQLCODE;
  9           v_errm := SUBSTR(SQLERRM, 1 , 64);
 10           DBMS_OUTPUT.PUT_LINE('The error code is ' || v_code || '- ' || v_errm);
 11  END;
 12  /
The error code is -1722- ORA-01722: invalid number

PL/SQL procedure successfully completed.

SQL>

Open in new window


Part of this code taken from Oracle Documentation:
10g: http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqlcode_function.htm
11g: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#LNPLS849
0
 

Author Closing Comment

by:Jay Redd
ID: 39799906
That did the trick . Thank you so much.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now