What Is the Equivalent For @@Error In Oracle?

9 minutes read

In Oracle, the equivalent for @@error in SQL Server is the SQLCODE function. SQLCODE returns the numeric error code for the last executed SQL statement. It is often used in conjunction with the SQLERRM function, which returns the error message associated with the error code. Together, SQLCODE and SQLERRM can be used to handle and display errors in Oracle PL/SQL code.

Best Oracle Database Books of September 2024

1
OCA Oracle Database SQL Exam Guide (Exam 1Z0-071) (Oracle Press)

Rating is 5 out of 5

OCA Oracle Database SQL Exam Guide (Exam 1Z0-071) (Oracle Press)

2
Oracle PL/SQL Programming: Covers Versions Through Oracle Database 12c

Rating is 4.9 out of 5

Oracle PL/SQL Programming: Covers Versions Through Oracle Database 12c

  • O Reilly Media
3
Oracle Database 12c PL/SQL Programming

Rating is 4.8 out of 5

Oracle Database 12c PL/SQL Programming

4
Beginning Oracle Database 12c Administration: From Novice to Professional

Rating is 4.7 out of 5

Beginning Oracle Database 12c Administration: From Novice to Professional

5
Expert Oracle Database Architecture: Techniques and Solutions for High Performance and Productivity

Rating is 4.6 out of 5

Expert Oracle Database Architecture: Techniques and Solutions for High Performance and Productivity

6
Expert Oracle Database Architecture

Rating is 4.5 out of 5

Expert Oracle Database Architecture

  • Apress
7
Oracle Database Application Security: With Oracle Internet Directory, Oracle Access Manager, and Oracle Identity Manager

Rating is 4.4 out of 5

Oracle Database Application Security: With Oracle Internet Directory, Oracle Access Manager, and Oracle Identity Manager

8
Oracle Database 12c PL/SQL Advanced Programming Techniques

Rating is 4.3 out of 5

Oracle Database 12c PL/SQL Advanced Programming Techniques

9
Oracle Database 11g SQL (Oracle Press)

Rating is 4.2 out of 5

Oracle Database 11g SQL (Oracle Press)

10
Oracle 12c For Dummies

Rating is 4.1 out of 5

Oracle 12c For Dummies


How to rollback transactions in case of errors in Oracle?

To rollback a transaction in Oracle in case of errors, you can use the ROLLBACK statement. Here is how you can rollback a transaction when an error occurs:

  1. Use a BEGIN...END block to start a transaction and catch any errors:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
BEGIN
  -- Start transaction
  INSERT INTO table_name (column1, column2) VALUES (value1, value2);
  -- Commit the transaction
  COMMIT;
EXCEPTION
  -- Rollback the transaction if an error occurs
  WHEN OTHERS THEN
    ROLLBACK;
END;


  1. Alternatively, you can use the SAVEPOINT statement to create a savepoint in the transaction and rollback to that savepoint if an error occurs. Here is an example:
1
2
3
4
5
6
SAVEPOINT start_transaction;
-- Start transaction
INSERT INTO table_name (column1, column2) VALUES (value1, value2);

-- If an error occurs, rollback to the savepoint
ROLLBACK TO start_transaction;


Remember to always rollback a transaction in case of errors to ensure data consistency and integrity.


How to raise custom errors in Oracle?

In Oracle PL/SQL, you can raise custom errors using the RAISE_APPLICATION_ERROR procedure. This procedure allows you to raise an error with a user-defined error code and message.


Here is an example of how to use the RAISE_APPLICATION_ERROR procedure to raise a custom error:

1
2
3
4
5
6
DECLARE
   custom_error EXCEPTION;
BEGIN
   RAISE custom_error;
END;
/


In this example, we declare a custom error named custom_error and then use the RAISE statement to raise the error. You can also provide an error number and error message as arguments to the RAISE statement, like this:

1
2
3
4
5
6
DECLARE
   custom_error EXCEPTION;
BEGIN
   RAISE_APPLICATION_ERROR(-20001, 'This is a custom error message.');
END;
/


In this example, we are raising a custom error with an error code of -20001 and a custom error message.


You can then handle these custom errors in your PL/SQL blocks using exception handling techniques.


What is the difference between RAISE_APPLICATION_ERROR and @@error in Oracle?

RAISE_APPLICATION_ERROR is a built-in procedure in Oracle PL/SQL that allows you to raise a custom exception with a user-defined error code and message. This can be used to handle errors and communicate specific error messages to the user or application.


On the other hand, @@error is a system function in SQL Server that allows you to retrieve the error number generated by the last executed statement. It is used to check for errors in T-SQL code and take appropriate actions based on the error number.


In summary, RAISE_APPLICATION_ERROR is used to raise custom exceptions with specific error codes and messages in PL/SQL, while @@error is used to retrieve error numbers in SQL Server.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

In Rust, you can define your own custom error types to handle and propagate errors in a type-safe manner. Custom error types allow you to have fine-grained control over error handling, provide additional information about errors, and make error handling more e...
In Oracle, the equivalent of the object_name() function would be the USER_OBJECTS view. This view provides information about objects owned by the current user, including tables, views, procedures, functions, packages, and more. By querying the USER_OBJECTS vie...
In Go, error handling is a common practice to handle and manage errors that can occur during the execution of a program. The language provides several mechanisms for handling errors effectively.Go uses a simple error model where an error is represented by the ...