Presentation is loading. Please wait.

Presentation is loading. Please wait.

EE Copyright س Oracle Corporation, 2000. All rights reserved. ® Review of PL/SQL.

Similar presentations


Presentation on theme: "EE Copyright س Oracle Corporation, 2000. All rights reserved. ® Review of PL/SQL."— Presentation transcript:

1 EE Copyright س Oracle Corporation, 2000. All rights reserved. ® Review of PL/SQL

2 E-2 Copyright س Oracle Corporation, 2000. All rights reserved. ® Block Structure for Anonymous PL/SQL Blocks DECLARE (optional) Declare PL/SQL objects to be used within this block BEGIN (mandatory) Define the executable statements EXCEPTION (optional) Define the actions that take place if an error arises END; (mandatory) DECLARE (optional) Declare PL/SQL objects to be used within this block BEGIN (mandatory) Define the executable statements EXCEPTION (optional) Define the actions that take place if an error arises END; (mandatory)

3 E-3 Copyright س Oracle Corporation, 2000. All rights reserved. ® Declaring PL/SQL Variables SyntaxExamplesSyntaxExamples identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_locationVARCHAR2(13) := 'Atlanta'; c_commCONSTANT NUMBER := 1400; v_countBINARY_INTEGER := 0; v_validBOOLEAN NOT NULL := TRUE; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_locationVARCHAR2(13) := 'Atlanta'; c_commCONSTANT NUMBER := 1400; v_countBINARY_INTEGER := 0; v_validBOOLEAN NOT NULL := TRUE;

4 E-4 Copyright س Oracle Corporation, 2000. All rights reserved. ® Declaring Variables with the %TYPE Attribute Examples... v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10;... v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10;...

5 E-5 Copyright س Oracle Corporation, 2000. All rights reserved. ® Creating a PL/SQL Record Declare variables to store the name, job, and salary of a new employee. Example Declare variables to store the name, job, and salary of a new employee. Example... TYPE emp_record_type IS RECORD (enameVARCHAR2(10), job VARCHAR2(9), salNUMBER(7,2)); emp_recordemp_record_type;... TYPE emp_record_type IS RECORD (enameVARCHAR2(10), job VARCHAR2(9), salNUMBER(7,2)); emp_recordemp_record_type;...

6 E-6 Copyright س Oracle Corporation, 2000. All rights reserved. ® The %ROWTYPE Attribute Examples Declare a variable to store the same information about a department as it is stored in the DEPT table. Declare a variable to store the same information about a employee as it is stored in the EMP table. Examples Declare a variable to store the same information about a department as it is stored in the DEPT table. Declare a variable to store the same information about a employee as it is stored in the EMP table. dept_recorddept%ROWTYPE; emp_recordemp%ROWTYPE;

7 E-7 Copyright س Oracle Corporation, 2000. All rights reserved. ® SELECT Statements in PL/SQL INTO clause is required. Example INTO clause is required. Example DECLARE v_deptnoNUMBER(2); v_locVARCHAR2(15); BEGIN SELECTdeptno, loc INTOv_deptno, v_loc FROMdept WHEREdname = 'SALES';... END; DECLARE v_deptnoNUMBER(2); v_locVARCHAR2(15); BEGIN SELECTdeptno, loc INTOv_deptno, v_loc FROMdept WHEREdname = 'SALES';... END;

8 E-8 Copyright س Oracle Corporation, 2000. All rights reserved. ® Inserting Data Add new employee information to the EMP table. Example Add new employee information to the EMP table. Example DECLARE v_empnoemp.empno%TYPE; BEGIN SELECTempno_sequence.NEXTVAL INTOv_empno FROMdual; INSERT INTOemp(empno, ename, job, deptno) VALUES(v_empno, 'HARDING', 'CLERK', 10); END; DECLARE v_empnoemp.empno%TYPE; BEGIN SELECTempno_sequence.NEXTVAL INTOv_empno FROMdual; INSERT INTOemp(empno, ename, job, deptno) VALUES(v_empno, 'HARDING', 'CLERK', 10); END;

9 E-9 Copyright س Oracle Corporation, 2000. All rights reserved. ® Updating Data Increase the salary of all employees in the EMP table who are Analysts. Example Increase the salary of all employees in the EMP table who are Analysts. Example DECLARE v_sal_increase emp.sal%TYPE := 2000; BEGIN UPDATEemp SETsal = sal + v_sal_increase WHEREjob = 'ANALYST'; END;

10 E-10 Copyright س Oracle Corporation, 2000. All rights reserved. ® Deleting Data Delete rows that belong to department 10 from the EMP table. Example Delete rows that belong to department 10 from the EMP table. Example DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno; END; DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno; END;

11 E-11 Copyright س Oracle Corporation, 2000. All rights reserved. ® COMMIT and ROLLBACK Statements Initiate a transaction with the first DML command to follow a COMMIT or ROLLBACK. Use COMMIT and ROLLBACK SQL statements to terminate a transaction explicitly. Initiate a transaction with the first DML command to follow a COMMIT or ROLLBACK. Use COMMIT and ROLLBACK SQL statements to terminate a transaction explicitly.

12 E-12 Copyright س Oracle Corporation, 2000. All rights reserved. ® IF-THEN-ELSIF Statements For a given value entered, return a calculated value. Example For a given value entered, return a calculated value. Example... IF v_start > 100 THEN v_start := 2 * v_start; ELSIF v_start >= 50 THEN v_start :=.5 * v_start; ELSE v_start :=.1 * v_start; END IF;... IF v_start > 100 THEN v_start := 2 * v_start; ELSIF v_start >= 50 THEN v_start :=.5 * v_start; ELSE v_start :=.1 * v_start; END IF;...

13 E-13 Copyright س Oracle Corporation, 2000. All rights reserved. ® Basic Loop DECLARE v_ordiditem.ordid%TYPE := 101; v_counterNUMBER(2) := 1; BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; DECLARE v_ordiditem.ordid%TYPE := 101; v_counterNUMBER(2) := 1; BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; Example

14 E-14 Copyright س Oracle Corporation, 2000. All rights reserved. ® FOR Loop Insert the first 10 new line items for order number 101. Example Insert the first 10 new line items for order number 101. Example DECLARE v_ordiditem.ordid%TYPE := 101; BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP; END; DECLARE v_ordiditem.ordid%TYPE := 101; BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP; END;

15 E-15 Copyright س Oracle Corporation, 2000. All rights reserved. ® Trapping Predefined Oracle Server Errors Reference the standard name in the exception- handling routine. Sample predefined exceptions: –NO_DATA_FOUND –TOO_MANY_ROWS –INVALID_CURSOR –ZERO_DIVIDE –DUP_VAL_ON_INDEX Reference the standard name in the exception- handling routine. Sample predefined exceptions: –NO_DATA_FOUND –TOO_MANY_ROWS –INVALID_CURSOR –ZERO_DIVIDE –DUP_VAL_ON_INDEX

16 E-16 Copyright س Oracle Corporation, 2000. All rights reserved. ® Predefined Exception BEGIN SELECT... COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2; WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3; END; Syntax

17 E-17 Copyright س Oracle Corporation, 2000. All rights reserved. ® DECLARE e_products_invalidEXCEPTION; PRAGMA EXCEPTION_INIT ( e_products_invalid, -2292); v_message VARCHAR2(50); BEGIN... EXCEPTION WHEN e_products_invalid THEN :g_message := 'Product code specified is not valid.';... END; DECLARE e_products_invalidEXCEPTION; PRAGMA EXCEPTION_INIT ( e_products_invalid, -2292); v_message VARCHAR2(50); BEGIN... EXCEPTION WHEN e_products_invalid THEN :g_message := 'Product code specified is not valid.';... END; Nonpredefined Error Trap for Oracle Server error number -2292, an integrity constraint violation Trap for Oracle Server error number -2292, an integrity constraint violation e_products_invalid EXCEPTION; 1 PRAGMA EXCEPTION_INIT ( e_products_invalid, -2292); 2 e_products_invalid 3

18 E-18 Copyright س Oracle Corporation, 2000. All rights reserved. ® User-Defined Exception [DECLARE] e_amount_remaining EXCEPTION;... BEGIN... RAISE e_amount_remaining;... EXCEPTION WHEN e_amount_remaining THEN :g_message := 'There is still an amount in stock.';... END; [DECLARE] e_amount_remaining EXCEPTION;... BEGIN... RAISE e_amount_remaining;... EXCEPTION WHEN e_amount_remaining THEN :g_message := 'There is still an amount in stock.';... END; ExampleExample e_amount_remaining EXCEPTION; 1 RAISE e_amount_remaining; 2 e_amount_remaining 3

19 E-19 Copyright س Oracle Corporation, 2000. All rights reserved. ® RAISE_APPLICATION_ERROR Syntax A procedure that lets you issue user-defined error messages from stored subprograms. Syntax A procedure that lets you issue user-defined error messages from stored subprograms. raise_application_error (error_number, message[, {TRUE | FALSE}]); raise_application_error (error_number, message[, {TRUE | FALSE}]);

20 E-20 Copyright س Oracle Corporation, 2000. All rights reserved. ® RAISE_APPLICATION_ERROR Used in two different places: –Executable section –Exception section Returns error conditions to the user in a manner consistent with other Oracle Server errors Used in two different places: –Executable section –Exception section Returns error conditions to the user in a manner consistent with other Oracle Server errors


Download ppt "EE Copyright س Oracle Corporation, 2000. All rights reserved. ® Review of PL/SQL."

Similar presentations


Ads by Google