Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-21
2
Medium Priority
?
586 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Steve Wales earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
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.

688 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