12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Handling PL/<strong>SQL</strong> Errors7.3.16 Using Bind VariablesWhen you embed an INSERT, UPDATE, DELETE, or SELECT <strong>SQL</strong> statement directly inyour PL/<strong>SQL</strong> code, PL/<strong>SQL</strong> turns the variables in the WHERE and VALUES clauses intobind variables automatically. <strong>Oracle</strong> can reuse these <strong>SQL</strong> statement each time the samecode is executed. To run similar statements with different variable values, you can saveparsing overhead by calling a stored procedure that accepts parameters, then issuesthe statements with the parameters substituted in the appropriate places.You do need to specify bind variables with dynamic <strong>SQL</strong>, in clauses like WHERE andVALUES where you normally use variables. Instead of concatenating literals andvariable values into a single string, replace the variables with the names of bindvariables (prefixed by a colon) and specify the corresponding PL/<strong>SQL</strong> variables withthe USING clause. Using the USING clause, instead of concatenating the variables intothe string, reduces parsing overhead and lets <strong>Oracle</strong> reuse the <strong>SQL</strong> statements.In Example 7–28, :1 and :2 are bind variables for amount and column_value. In thesame example, there are additional bind variables, such as :cvalue, :cname, and:amt.7.4 Handling PL/<strong>SQL</strong> ErrorsPL/<strong>SQL</strong> makes it easy to detect and process error conditions known as exceptions.When an error occurs, an exception is raised: normal execution stops and controltransfers to special exception-handling code, which comes at the end of any PL/<strong>SQL</strong>block. Each different exception is processed by a particular exception handler.PL/<strong>SQL</strong> exception handling is different from the manual checking you might be usedto from C programming, where you insert a check to make sure that every operationsucceeded. Instead, the checks and calls to error routines are performed automatically,similar to the exception mechanism in Java programming.Predefined exceptions are raised automatically for certain common error conditionsinvolving variables or database operations. For example, if you try to divide a numberby zero, PL/<strong>SQL</strong> raises the predefined exception ZERO_DIVIDE automatically. See"Summary of Predefined PL/<strong>SQL</strong> Exceptions" on page 7-21.You can declare exceptions of your own, for conditions that you decide are errors, or tocorrespond to database errors that normally result in ORA- error messages. When youdetect a user-defined error condition, you execute a RAISE statement. See "DeclaringPL/<strong>SQL</strong> Exceptions" on page 7-23.This topic includes the following topics:■ Summary of Predefined PL/<strong>SQL</strong> Exceptions■ Using the Exception Handler■ Declaring PL/<strong>SQL</strong> Exceptions■ Scope Rules for PL/<strong>SQL</strong> Exceptions■ Continuing After an Exception is Raised7.4.1 Summary of Predefined PL/<strong>SQL</strong> ExceptionsAn internal exception is raised automatically if your PL/<strong>SQL</strong> program violates an<strong>Oracle</strong> rule or exceeds a system-dependent limit. PL/<strong>SQL</strong> predefines some common<strong>Oracle</strong> errors as exceptions. For example, PL/<strong>SQL</strong> raises the predefined exception NO_DATA_FOUND if a SELECT INTO statement returns no rows.PL/<strong>SQL</strong>: Usage Information 7-21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!