주 오픈메이드컨설팅...

10
1 () 오픈메이드 컨설팅 오 동 규 수석 컨설턴트

Transcript of 주 오픈메이드컨설팅...

Page 1: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

1

(주) 오픈메이드 컨설팅

오 동 규 수석 컨설턴트

Page 2: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

2

User-Defined Exceptions : 개발자가 특수한 조건을지정하여 Exception 을 정의할수 있음. 제약사항:SQLCODE 와 SQLERRM 를 정의할수 없음. RAISE 명령어를 사용해야함.반드시 EXCEPTION 절이 필요함.(예약어 처럼 사용가능함)

DECLAREv_student_id STUDENT.STUDENT_ID%TYPE := &sv_student_id; e_invalid_id EXCEPTION;

BEGINIF v_student_id < 0 THEN

RAISE e_invalid_id;END IF;

EXCEPTIONWHEN e_invalid_id THEN

DBMS_OUTPUT.PUT_LINE ('An id cannot be negative');END;

Page 3: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

3

-- outer blockBEGIN

-- inner blockDECLARE

e_my_exception EXCEPTION;BEGIN

DBMS_OUTPUT.PUT_LINE ('Inner block');EXCEPTION

WHEN e_my_exception THENDBMS_OUTPUT.PUT_LINE ('An error has occurred');

END;

IF 10 > &sv_number THENRAISE e_my_exception;

END IF;END;

에러발생PLS-00201: identifier 'E_MY_EXCEPTION' must be declared

Page 4: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

4

RAISE_APPLICATION_ERROR :•사용자가 SQLCODE 와 SQLERRM 을 정의 할수 있는 기능. •미리 정의되지 않은 SQLCODE 와 SQLERRM 를 생성함.•EXCEPTION 절이 필요없음.

제약사항:•SQLCODE 는 -20,999 and -20,000 사이만 가능함.•RAISE 명령어를 사용할수 없음.•EXCEPTION 명을 지정할수 없음.

DECLAREv_student_id STUDENT.STUDENT_ID%TYPE := &sv_student_id;

BEGINIF v_student_id < 0 THEN

RAISE_APPLICATION_ERROR (-20000, 'An id cannot be negative');

END IF;END;

Page 5: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

5

EXCEPTION_INIT Pragma : •User-Defined Exception 을 미리 정의된 SQLCODE 와 연결시키는기능을 한다.•Block 내에서는 예약어(Built In Exceptions) 처럼 사용이 가능하다.

제약사항:

•RAISE 명령어를 사용할수 없음.

DECLAREv_zip ZIPCODE.ZIP%TYPE := '&sv_zip';e_child_exists EXCEPTION;PRAGMA EXCEPTION_INIT(e_child_exists, -2292);

BEGINDELETE FROM zipcodeWHERE zip = v_zip;

COMMIT;EXCEPTION

WHEN e_child_exists THENDBMS_OUTPUT.PUT_LINE ('Delete students for this zipcode first');

END;

Page 6: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

6

Name of exception Oracle errorSQLCODE

Description

CURSOR_ALREADY_OPEN ORA-6511 SQLCODE=-6511

You tried to OPEN a cursor that was already OPEN. You must CLOSE a cursor before you try to OPEN or re-OPEN it.

DUP_VAL_ON_INDEX ORA-00001 SQLCODE= -1

Your INSERT or UPDATE statement attempted to store duplicate values in a column or columns in a row that is restricted by a unique index.

INVALID_CURSOR ORA-01001 SQLCODE=-1001

You made reference to a cursor that did not exist. This usually happens when you try to FETCH from a cursor or CLOSE a cursor before that cursor is OPENed.

INVALID_NUMBER ORA-01722 SQLCODE =-1722

PL/SQL executes a SQL statement that cannot convert a character string successfully to a number. This exception is different from the VALUE_ERROR exception because it is raised only from within a SQL statement.

LOGIN_DENIED ORA-01017 SQLCODE= -1017

Your program tried to log into the Oracle RDBMS with an invalid username-password combination. This exception is usually encountered when you embed PL/SQL in a 3GL language.

NO_DATA_FOUND ORA-01403 SQLCODE= +100

This exception is raised in three different scenarios: (1) You executed a SELECT INTO statement (implicit cursor) that returned no rows. (2) You referenced an uninitialized row in a local PL/SQL table. (3) You read past end-of-file with the UTL_FILE package.

NOT_LOGGED ON ORA-01012 SQLCODE= -1012

Your program tried to execute a call to the database (usually with a DML statement) before it had logged into the Oracle RDBMS.

Page 7: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

7

Name of exception Oracle errorSQLCODE

Description

PROGRAM_ERROR ORA-06501 SQLCODE= -6501

PL/SQL encounters an internal problem. The message text usually also tells you to "Contact Oracle Support."

STORAGE_ERROR ORA-06500 SQLCODE= -6500

Your program ran out of memory, or memory was in some way corrupted.

TIMEOUT_ON_RESOURCE ORA-00051 SQLCODE=-51

A timeout occurred in the RDBMS while waiting for a resource.

TOO_MANY_ROWS ORA-01422 SQLCODE= -1422

A SELECT INTO statement returned more than one row. A SELECT INTO can return only one row; if your SQL statement returns more than one row, you should place the SELECT statement in an explicit CURSOR declaration and FETCH from that cursor one row at a time.

TRANSACTION_BACKED_OUT ORA-00061 SQLCODE= -61

The remote part of a transaction is rolled back, either with an explicit ROLLBACK command or as the result of some other action (such as a failed SQL/DML on the remote database).

VALUE_ERROR ORA-06502 SQLCODE= -6502

PL/SQL encountered an error having to do with the conversion, truncation, or invalid constraining of numeric and character data. This is a very general and common exception. If this type of error is encountered in a SQL DML statement within a PL/SQL block, then the INVALID_NUMBER exception is raised.

ZERO_DIVIDE ORA-01476 SQLCODE= -1476

Your program tried to divide

Page 8: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

8

SET SERVEROUTPUT ONDECLARE

vr_student student%ROWTYPE;BEGIN

SELECT *INTO vr_studentFROM studentWHERE student_id = 156;

DBMS_OUTPUT.PUT_LINE (vr_student.first_name||' '||vr_student.last_name||' has an ID of 156');

EXCEPTIONWHEN no_data_found

THENDBMS_OUTPUT.PUT_LINE('The Student id is not valid’)

END;

Page 9: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

9

Exceptions-정리

Built In Exceptions : • 미리 SQLCODE 와 SQLERRM 을 오라클이 만들어서 정의한 것.• RAISE 명령어를 사용할 수 없음.• 반드시 EXCEPTION 절이 필요함.User-Defined Exceptions : • 개발자가 특수한 조건을 지정하여 Exception 을 정의함. • SQLCODE 와 SQLERRM 를 정의할 수 없음.• RAISE 명령어를 사용해야 함.• 반드시 EXCEPTION 절이 필요함.RAISE_APPLICATION_ERROR :• 사용자가 특수한 조건에서 SQLCODE 와 SQLERRM 을 정의 할수 있음. • SQLCODE 는 -20,999 and -20,000 사이의 수로 정의해야 함.• RAISE 명령어를 사용할 수 없음.• EXCEPTION 절이 필요 없음.EXCEPTION_INIT Pragma : • User-Defined Exceptions 를 미리 정의된 SQLCODE 와 연결시키는 기능을 한다.• Block 내에서는 Built In Exceptions 와 같이 사용가능.• RAISE 명령어를 사용할 수 없음. • 반드시 EXCEPTION 절이 필요함.

Built In Exceptions와 User-Defined Exceptions,RAISE_APPLICATION_ERROR , EXCEPTION_INIT Pragma 의차이점을 설명하라.

Page 10: 주 오픈메이드컨설팅 오동규수석컨설턴트scidb.tistory.com/attachment/cfile22.uf@151D140B4BE3B6D01E7374.pdf · • 사용자가특수한조건에서sqlcode 와sqlerrm

10

감사합니다.