+ All Categories
Home > Documents > EE Copyright س Oracle Corporation, 2000. All rights reserved. ® Review of PL/SQL.

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

Date post: 20-Jan-2016
Category:
Upload: terence-townsend
View: 212 times
Download: 0 times
Share this document with a friend
13
E E Copyright سOracle Corporation, 2000. All rights reserved. ® Review of PL/SQL
Transcript
Page 1: EE Copyright س Oracle Corporation, 2000. All rights reserved. ® Review of PL/SQL.

EEEE

Copyright س Oracle Corporation, 2000. All rights reserved.®

Review of PL/SQLReview of PL/SQL

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

E-E-22 Copyright س Oracle Corporation, 2000. All rights reserved.®

Block Structure for Anonymous PL/SQL Blocks

Block Structure for Anonymous PL/SQL Blocks

DECLARE (optional)

Declare PL/SQL objects to be usedwithin this block

BEGIN (mandatory)

Define the executable statements

EXCEPTION (optional)

Define the actions that take place ifan error arises

END; (mandatory)

DECLARE (optional)

Declare PL/SQL objects to be usedwithin this block

BEGIN (mandatory)

Define the executable statements

EXCEPTION (optional)

Define the actions that take place ifan error arises

END; (mandatory)

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

E-E-33 Copyright س Oracle Corporation, 2000. All rights reserved.®

Declaring PL/SQL VariablesDeclaring PL/SQL Variables

SyntaxSyntax

ExamplesExamples

SyntaxSyntax

ExamplesExamples

identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];

identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];

Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := 'Atlanta'; c_ comm CONSTANT NUMBER := 1400; v_count BINARY_INTEGER := 0; v_valid BOOLEAN NOT NULL := TRUE;

Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := 'Atlanta'; c_ comm CONSTANT NUMBER := 1400; v_count BINARY_INTEGER := 0; v_valid BOOLEAN NOT NULL := TRUE;

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

E-E-44 Copyright س Oracle Corporation, 2000. All rights reserved.®

Declaring Variables with the %TYPE Attribute

Declaring Variables with the %TYPE Attribute

ExamplesExamples

... v_ename emp.ename%TYPE; v_balance NUMBER(7,2); v_min_balance v_balance%TYPE := 10;...

... v_ename emp.ename%TYPE; v_balance NUMBER(7,2); v_min_balance v_balance%TYPE := 10;...

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

E-E-55 Copyright س Oracle Corporation, 2000. All rights reserved.®

Creating a PL/SQL RecordCreating a PL/SQL Record

Declare variables to store the name, job, and salary ofa new employee.

Example

Declare variables to store the name, job, and salary ofa new employee.

Example

... TYPE emp_record_type IS RECORD (ename VARCHAR2(10), job VARCHAR2(9), sal NUMBER(7,2)); emp_record emp_record_type;...

... TYPE emp_record_type IS RECORD (ename VARCHAR2(10), job VARCHAR2(9), sal NUMBER(7,2)); emp_record emp_record_type;...

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

E-E-66 Copyright س Oracle Corporation, 2000. All rights reserved.®

The %ROWTYPE AttributeThe %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_record dept%ROWTYPE; dept_record dept%ROWTYPE;

emp_record emp%ROWTYPE; emp_record emp%ROWTYPE;

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

E-E-77 Copyright س Oracle Corporation, 2000. All rights reserved.®

SELECT Statements in PL/SQLSELECT Statements in PL/SQL

INTO clause is required.

Example

INTO clause is required.

Example

DECLARE v_deptno NUMBER(2); v_loc VARCHAR2(15);BEGIN SELECT deptno, loc INTO v_deptno, v_loc FROM dept WHERE dname = 'SALES'; ...END;

DECLARE v_deptno NUMBER(2); v_loc VARCHAR2(15);BEGIN SELECT deptno, loc INTO v_deptno, v_loc FROM dept WHERE dname = 'SALES'; ...END;

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

E-E-1111 Copyright س Oracle Corporation, 2000. All rights reserved.®

COMMIT and ROLLBACK StatementsCOMMIT 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.

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

E-E-1212 Copyright س Oracle Corporation, 2000. All rights reserved.®

IF-THEN-ELSIF StatementsIF-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;. . .

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

E-E-1313 Copyright س Oracle Corporation, 2000. All rights reserved.®

Basic LoopBasic Loop

DECLARE v_ordid item.ordid%TYPE := 101; v_counter NUMBER(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_ordid item.ordid%TYPE := 101; v_counter NUMBER(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;

ExampleExample

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

E-E-1414 Copyright س Oracle Corporation, 2000. All rights reserved.®

FOR LoopFOR 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_ordid item.ordid%TYPE := 101;BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP;END;

DECLARE v_ordid item.ordid%TYPE := 101;BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP;END;

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

E-E-1515 Copyright س Oracle Corporation, 2000. All rights reserved.®

Trapping Predefined Oracle Server Errors

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

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

E-E-1616 Copyright س Oracle Corporation, 2000. All rights reserved.®

Predefined ExceptionPredefined 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;

SyntaxSyntax


Recommended