Solved

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

Posted on 2014-01-21
2
551 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

937 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

5 Experts available now in Live!

Get 1:1 Help Now