copyright oracle corporation, 1998. all rights reserved. 3 creating procedures

31
Copyright Oracle Corporation, 1998. All rights reserved. 3 3 Creating Procedures

Upload: grant-kelly

Post on 12-Jan-2016

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

Copyright Oracle Corporation, 1998. All rights reserved.

33

Creating ProceduresCreating Procedures

Page 2: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-2 Copyright Oracle Corporation, 1998. All rights reserved.

ObjectivesObjectives

After completing this lesson, you should After completing this lesson, you should be able to do the following:be able to do the following:

• Describe the uses of procedures

• Create client-side and server-side procedures

• Create procedures with arguments

• Invoke a procedure

• Remove a procedure

After completing this lesson, you should After completing this lesson, you should be able to do the following:be able to do the following:

• Describe the uses of procedures

• Create client-side and server-side procedures

• Create procedures with arguments

• Invoke a procedure

• Remove a procedure

Page 3: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-3 Copyright Oracle Corporation, 1998. All rights reserved.

Overview of ProceduresOverview of Procedures

• A procedure is a named PL/SQL block that performs an action.

• A procedure can be stored in the database, as a database object, for repeated execution.

• A procedure is a named PL/SQL block that performs an action.

• A procedure can be stored in the database, as a database object, for repeated execution.

Page 4: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-4 Copyright Oracle Corporation, 1998. All rights reserved.

Syntax for Creating ProceduresSyntax for Creating Procedures

CREATE [OR REPLACE] PROCEDURE procedure_name (argument1 [mode1] datatype1, argument2 [mode2] datatype2, . . .)IS|ASPL/SQL Block;

CREATE [OR REPLACE] PROCEDURE procedure_name (argument1 [mode1] datatype1, argument2 [mode2] datatype2, . . .)IS|ASPL/SQL Block;

Page 5: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-5 Copyright Oracle Corporation, 1998. All rights reserved.

Developing Stored ProceduresDeveloping Stored Procedures

SQL*PlusSQL*Plus CodeCodeOracleOracle

Procedure Procedure BuilderBuilder

CodeCodeEditorEditor

1

SQL> START file.sqlSave 2

Source codeSource code

Execute

p-codep-code

Compile

OracleOracle

Page 6: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-6 Copyright Oracle Corporation, 1998. All rights reserved.

Creating a Stored Procedure Using SQL*Plus

Creating a Stored Procedure Using SQL*Plus

1.1. Enter the text of the CREATE PROCEDUREEnter the text of the CREATE PROCEDUREstatement in a system editor or wordstatement in a system editor or wordprocessor and save it as a script file (processor and save it as a script file (.sql.sqlextension).extension).

2.2. From SQL*Plus, run the script file to From SQL*Plus, run the script file to compile the source code into p-code. Usecompile the source code into p-code. UseSHOWSHOW ERRORS to see any compilationERRORS to see any compilationerrors.errors.

3.3. When successfully compiled, invoke theWhen successfully compiled, invoke theprocedure from an Oracle Serverprocedure from an Oracle Serverenvironment.environment.

1.1. Enter the text of the CREATE PROCEDUREEnter the text of the CREATE PROCEDUREstatement in a system editor or wordstatement in a system editor or wordprocessor and save it as a script file (processor and save it as a script file (.sql.sqlextension).extension).

2.2. From SQL*Plus, run the script file to From SQL*Plus, run the script file to compile the source code into p-code. Usecompile the source code into p-code. UseSHOWSHOW ERRORS to see any compilationERRORS to see any compilationerrors.errors.

3.3. When successfully compiled, invoke theWhen successfully compiled, invoke theprocedure from an Oracle Serverprocedure from an Oracle Serverenvironment.environment.

Page 7: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-7 Copyright Oracle Corporation, 1998. All rights reserved.

Creating a Procedure Using Procedure Builder

Creating a Procedure Using Procedure Builder

Procedure Builder allows you to:Procedure Builder allows you to:

• Create a server-side procedure

• Create a client-side procedure

• Drag and drop procedures betweenclient and server

Procedure Builder allows you to:Procedure Builder allows you to:

• Create a server-side procedure

• Create a client-side procedure

• Drag and drop procedures betweenclient and server

Page 8: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-8 Copyright Oracle Corporation, 1998. All rights reserved.

Creating Server-Side Procedures Using Procedure Builder

Creating Server-Side Procedures Using Procedure Builder

CreateCreateDeleteDelete

Page 9: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-9 Copyright Oracle Corporation, 1998. All rights reserved.

Creating Client-Side Procedures Using Procedure Builder

Creating Client-Side Procedures Using Procedure Builder

Page 10: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-10 Copyright Oracle Corporation, 1998. All rights reserved.

Navigating Compilation Errorsin Procedure Builder

Navigating Compilation Errorsin Procedure Builder

Page 11: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-11 Copyright Oracle Corporation, 1998. All rights reserved.

Procedural Parameter ModesProcedural Parameter Modes

CallingCallingenvironmentenvironment

ProcedureProcedure

(DECLARE)(DECLARE)

BEGINBEGIN

EXCEPTIONEXCEPTION

END;END;

IN parameterIN parameter

OUT parameter OUT parameter

IN OUT parameterIN OUT parameter

Page 12: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-12 Copyright Oracle Corporation, 1998. All rights reserved.

IN OUT

Must be specified

Passed into subprogram; returned to calling environment

Initialized variable

Must be a variable

OUT

Must be specified

Returned to calling environment

Uninitialized variable

Must be a variable

Parameter Modes for Formal Parameters

Parameter Modes for Formal Parameters

IN

Default

Value ispassed into subprogram

Formal parameter acts as a constant

Actual parametercan be a literal, expression, constant, orinitialized variable

Page 13: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-13 Copyright Oracle Corporation, 1998. All rights reserved.

IN Parameters: ExampleIN Parameters: Example

SQL> CREATE OR REPLACE PROCEDURE raise_salary 2 (v_id in emp.empno%TYPE) 3 IS 4 BEGIN 5 UPDATE emp 6 SET sal = sal * 1.10 7 WHERE empno = v_id; 8 END raise_salary; 9 /Procedure created.Procedure created.

SQL> EXECUTE raise_salary (7369)PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> CREATE OR REPLACE PROCEDURE raise_salary 2 (v_id in emp.empno%TYPE) 3 IS 4 BEGIN 5 UPDATE emp 6 SET sal = sal * 1.10 7 WHERE empno = v_id; 8 END raise_salary; 9 /Procedure created.Procedure created.

SQL> EXECUTE raise_salary (7369)PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

v_idv_id73697369

Page 14: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-14 Copyright Oracle Corporation, 1998. All rights reserved.

OUT Parameters: ExampleOUT Parameters: Example

Calling environmentCalling environment QUERY_EMP procedureQUERY_EMP procedure

76547654 v_idv_id

v_namev_name

v_salaryv_salary

v_commv_comm

MARTINMARTIN

12501250

14001400

Page 15: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-15 Copyright Oracle Corporation, 1998. All rights reserved.

OUT Parameters: ExampleOUT Parameters: Example

SQL> CREATE OR REPLACE PROCEDURE query_emp 1 (v_id IN emp.empno%TYPE, 2 v_name OUT emp.ename%TYPE, 3 v_salary OUT emp.sal%TYPE, 4 v_comm OUT emp.comm%TYPE) 5 IS 6 BEGIN 7 SELECT ename, sal, comm 8 INTO v_name, v_salary, v_comm 9 FROM emp 10 WHERE empno = v_id; 11 END query_emp; 12 /

SQL> CREATE OR REPLACE PROCEDURE query_emp 1 (v_id IN emp.empno%TYPE, 2 v_name OUT emp.ename%TYPE, 3 v_salary OUT emp.sal%TYPE, 4 v_comm OUT emp.comm%TYPE) 5 IS 6 BEGIN 7 SELECT ename, sal, comm 8 INTO v_name, v_salary, v_comm 9 FROM emp 10 WHERE empno = v_id; 11 END query_emp; 12 /

Page 16: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-16 Copyright Oracle Corporation, 1998. All rights reserved.

OUT Parameters and SQL*PlusOUT Parameters and SQL*Plus

SQL> START emp_query.sqlProcedure created.Procedure created.

SQL> START emp_query.sqlProcedure created.Procedure created.

SQL> VARIABLE g_name VARCHAR2(15)SQL> VARIABLE g_sal NUMBERSQL> VARIABLE g_comm NUMBER

SQL> VARIABLE g_name VARCHAR2(15)SQL> VARIABLE g_sal NUMBERSQL> VARIABLE g_comm NUMBER

SQL> EXECUTE query_emp(7654,:g_name,:g_sal,:g_comm)PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> EXECUTE query_emp(7654,:g_name,:g_sal,:g_comm)PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> PRINT g_nameG_NAME---------------MARTIN

SQL> PRINT g_nameG_NAME---------------MARTIN

Page 17: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-17 Copyright Oracle Corporation, 1998. All rights reserved.

PL/SQL> .CREATE CHAR g_name LENGTH 10 PL/SQL> .CREATE NUMBER g_sal PRECISION 4PL/SQL> .CREATE NUMBER g_comm PRECISION 4PL/SQL> QUERY_EMP (7654, :g_name, :g_sal, +> :g_comm);PL/SQL> TEXT_IO.PUT_LINE (:g_name || ' earns ' || +> TO_CHAR(:g_sal) || ' and a commission of ' +> || TO_CHAR(:g_comm));MARTIN earns 1250 and a commission of 1400MARTIN earns 1250 and a commission of 1400

OUT Parameters and Procedure Builder

OUT Parameters and Procedure Builder

Page 18: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-18 Copyright Oracle Corporation, 1998. All rights reserved.

IN OUT ParametersIN OUT Parameters

SQL> CREATE OR REPLACE PROCEDURE format_phone 2 (v_phone_no IN OUT VARCHAR2) 3 IS 4 BEGIN 5 v_phone_no := '(' || SUBSTR(v_phone_no,1,3) || 6 ')' || SUBSTR(v_phone_no,4,3) || 7 '-' || SUBSTR(v_phone_no,7); 8 END format_phone; 9 /

Calling environmentCalling environment FORMAT_PHONE procedureFORMAT_PHONE procedure

v_phone_nov_phone_no'(800)633-0575' '(800)633-0575' '8006330575' '8006330575'

Page 19: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-19 Copyright Oracle Corporation, 1998. All rights reserved.

Invoking FORMAT_PHONEfrom SQL*Plus

Invoking FORMAT_PHONEfrom SQL*Plus

SQL>VARIABLE g_phone_no VARCHAR2(15)

SQL> BEGIN :g_phone_no := '8006330575'; END; 2 /PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.SQL> PRINT g_phone_noG_PHONE_NO---------------8006330575

SQL>VARIABLE g_phone_no VARCHAR2(15)

SQL> BEGIN :g_phone_no := '8006330575'; END; 2 /PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.SQL> PRINT g_phone_noG_PHONE_NO---------------8006330575

SQL> EXECUTE format_phone (:g_phone_no)PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> PRINT g_phone_noG_PHONE_NO---------------(800)633-0575

SQL> EXECUTE format_phone (:g_phone_no)PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> PRINT g_phone_noG_PHONE_NO---------------(800)633-0575

Page 20: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-20 Copyright Oracle Corporation, 1998. All rights reserved.

Invoking FORMAT_PHONE from Procedure Builder

Invoking FORMAT_PHONE from Procedure Builder

PL/SQL> .CREATE CHAR g_phone_no LENGTH 15 PL/SQL> BEGIN +> :g_phone_no := '8006330575'; +> END;PL/SQL> FORMAT_PHONE (:g_phone_no);PL/SQL> TEXT_IO.PUT_LINE (:g_phone_no);(800)633-0575(800)633-0575

PL/SQL> .CREATE CHAR g_phone_no LENGTH 15 PL/SQL> BEGIN +> :g_phone_no := '8006330575'; +> END;PL/SQL> FORMAT_PHONE (:g_phone_no);PL/SQL> TEXT_IO.PUT_LINE (:g_phone_no);(800)633-0575(800)633-0575

Page 21: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-21 Copyright Oracle Corporation, 1998. All rights reserved.

Methods for Passing Parameters

Methods for Passing Parameters

• Positional

• Named

• Combination

• Positional

• Named

• Combination

Page 22: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-22 Copyright Oracle Corporation, 1998. All rights reserved.

DEFAULT Option for Parameters

DEFAULT Option for Parameters

SQL> CREATE OR REPLACE PROCEDURE add_dept 1 (v_name IN dept.dname%TYPE DEFAULT 'unknown', 2 v_loc IN dept.loc%TYPE DEFAULT 'unknown') 3 IS 4 BEGIN 5 INSERT INTO dept 6 VALUES (dept_deptno.NEXTVAL, v_name, v_loc); 7 END add_dept; 8 /

SQL> CREATE OR REPLACE PROCEDURE add_dept 1 (v_name IN dept.dname%TYPE DEFAULT 'unknown', 2 v_loc IN dept.loc%TYPE DEFAULT 'unknown') 3 IS 4 BEGIN 5 INSERT INTO dept 6 VALUES (dept_deptno.NEXTVAL, v_name, v_loc); 7 END add_dept; 8 /

Page 23: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-23 Copyright Oracle Corporation, 1998. All rights reserved.

Examples of Passing ParametersExamples of Passing Parameters

SQL> BEGIN 2 add_dept; 3 add_dept ( 'TRAINING', 'NEW YORK'); 4 add_dept ( v_loc => 'DALLAS', v_name =>'EDUCATION'); 5 add_dept ( v_loc => 'BOSTON') ; 6 END; 7 /PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> BEGIN 2 add_dept; 3 add_dept ( 'TRAINING', 'NEW YORK'); 4 add_dept ( v_loc => 'DALLAS', v_name =>'EDUCATION'); 5 add_dept ( v_loc => 'BOSTON') ; 6 END; 7 /PL/SQL procedure successfully completed.PL/SQL procedure successfully completed.

SQL> SELECT * FROM dept;

DEPTNO DNAME LOC------ -------------- ------------- ... ... ... 41 unknown unknown 42 TRAINING NEW YORK 43 EDUCATION DALLAS 44 unknown BOSTON

SQL> SELECT * FROM dept;

DEPTNO DNAME LOC------ -------------- ------------- ... ... ... 41 unknown unknown 42 TRAINING NEW YORK 43 EDUCATION DALLAS 44 unknown BOSTON

Page 24: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-24 Copyright Oracle Corporation, 1998. All rights reserved.

Invoking a Procedure from an Anonymous PL/SQL Block

Invoking a Procedure from an Anonymous PL/SQL Block

DECLARE v_id NUMBER := 7900;BEGIN raise_salary(v_id); --invoke procedure COMMIT;...

END;

DECLARE v_id NUMBER := 7900;BEGIN raise_salary(v_id); --invoke procedure COMMIT;...

END;

Page 25: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-25 Copyright Oracle Corporation, 1998. All rights reserved.

Invoking a Procedure from a Stored Procedure

Invoking a Procedure from a Stored Procedure

SQL> CREATE OR REPLACE PROCEDURE process_emps 2 IS 3 CURSOR emp_cursor IS 4 SELECT empno 5 FROM emp; 6 BEGIN 7 FOR emp_rec IN emp_cursor 8 LOOP 9 raise_salary(emp_rec.empno); --invoke procedure 10 END LOOP; 11 COMMIT; 12 END process_emps; 13 /

SQL> CREATE OR REPLACE PROCEDURE process_emps 2 IS 3 CURSOR emp_cursor IS 4 SELECT empno 5 FROM emp; 6 BEGIN 7 FOR emp_rec IN emp_cursor 8 LOOP 9 raise_salary(emp_rec.empno); --invoke procedure 10 END LOOP; 11 COMMIT; 12 END process_emps; 13 /

Page 26: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-26 Copyright Oracle Corporation, 1998. All rights reserved.

Removing ProceduresRemoving Procedures

• Using SQL*Plus:

Drop a server-side procedure

• Using Procedure Builder:

– Drop a server-side procedure

– Delete a client-side procedure

• Using SQL*Plus:

Drop a server-side procedure

• Using Procedure Builder:

– Drop a server-side procedure

– Delete a client-side procedure

Page 27: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-27 Copyright Oracle Corporation, 1998. All rights reserved.

Removing Server-Side Procedures

Removing Server-Side Procedures

Using SQL*Plus:Using SQL*Plus:

• Syntax

• Example

Using SQL*Plus:Using SQL*Plus:

• Syntax

• Example

DROP PROCEDURE procedure_nameDROP PROCEDURE procedure_name

SQL> DROP PROCEDURE raise_salary;Procedure dropped.

SQL> DROP PROCEDURE raise_salary;Procedure dropped.

Page 28: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-28 Copyright Oracle Corporation, 1998. All rights reserved.

Removing Server-Side Procedures

Removing Server-Side Procedures

Using Procedure Builder:Using Procedure Builder:

1.1. Connect to the database.Connect to the database.

2.2. Expand the Database Objects node.Expand the Database Objects node.

3.3. Expand the schema of the owner of the Expand the schema of the owner of the procedure.procedure.

4.4. Expand the Stored Program Units node.Expand the Stored Program Units node.

5.5. Click the procedure you want to drop.Click the procedure you want to drop.

6.6. Click Delete in the Object Navigator.Click Delete in the Object Navigator.

7. 7. Click Yes to confirm.Click Yes to confirm.

Using Procedure Builder:Using Procedure Builder:

1.1. Connect to the database.Connect to the database.

2.2. Expand the Database Objects node.Expand the Database Objects node.

3.3. Expand the schema of the owner of the Expand the schema of the owner of the procedure.procedure.

4.4. Expand the Stored Program Units node.Expand the Stored Program Units node.

5.5. Click the procedure you want to drop.Click the procedure you want to drop.

6.6. Click Delete in the Object Navigator.Click Delete in the Object Navigator.

7. 7. Click Yes to confirm.Click Yes to confirm.

Page 29: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-29 Copyright Oracle Corporation, 1998. All rights reserved.

Removing Client-Side Procedures

Removing Client-Side Procedures

Using Procedure Builder:Using Procedure Builder:

1. 1. Expand the Program Units node.Expand the Program Units node.

2. 2. Click the procedure you want to Click the procedure you want to remove.remove.

3. 3. Click Delete in the Object Navigator.Click Delete in the Object Navigator.

4.4. Click Yes to confirm.Click Yes to confirm.

Using Procedure Builder:Using Procedure Builder:

1. 1. Expand the Program Units node.Expand the Program Units node.

2. 2. Click the procedure you want to Click the procedure you want to remove.remove.

3. 3. Click Delete in the Object Navigator.Click Delete in the Object Navigator.

4.4. Click Yes to confirm.Click Yes to confirm.

Page 30: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-30 Copyright Oracle Corporation, 1998. All rights reserved.

SummarySummary

• A procedure is a named PL/SQL block that performs an action.

• Use parameters to pass data from the calling environment to the procedure.

• Procedures can be invoked from any tool or language that supports PL/SQL.

• Procedures can serve as building blocks for an application.

• A procedure is a named PL/SQL block that performs an action.

• Use parameters to pass data from the calling environment to the procedure.

• Procedures can be invoked from any tool or language that supports PL/SQL.

• Procedures can serve as building blocks for an application.

Page 31: Copyright  Oracle Corporation, 1998. All rights reserved. 3 Creating Procedures

3-31 Copyright Oracle Corporation, 1998. All rights reserved.

Practice OverviewPractice Overview

Creating stored procedures to implement Creating stored procedures to implement a variety of data manipulation and query a variety of data manipulation and query routines routines

Creating stored procedures to implement Creating stored procedures to implement a variety of data manipulation and query a variety of data manipulation and query routines routines