creating stored procedures and functions. objectives after completing this lecture, you should be...

47
CREATING STORED PROCEDURES AND FUNCTIONS

Upload: ami-hall

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

CREATING STORED PROCEDURES AND FUNCTIONS

Page 2: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Objectives

After completing this lecture, you should be able to dothe following:

•Differentiate between anonymous blocks andsubprograms

•Create a simple procedure and invoke it from ananonymous block

•Create a simple function •Create a simple function that accepts a parameter

•Differentiate between procedures and functions

Page 3: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Procedures and Functions

Are named PL/SQL blocks. Are called PL/SQL subprograms.

Have block structures similar to anonymous blocks: Optional declarative section (without

DECLARE keyword). Mandatory executable section. Optional section to handle exceptions.

Page 4: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Differences Between Anonymous Blocks and Subprograms

Page 5: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Procedure , function

Procedure A program that performs one or more

actions and is called as an executable PL/SQL statement.

You can pass information into and out of a procedure through its parameter list.

Function A program that returns a single value and is

used just like a PL/ SQL expression. You can pass information into a function

through its parameter list.

Page 6: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Procedure

Page 7: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Procedure

A procedure is a module that performs one or more actions.

Because a procedure call is a standalone executable statement in PL/SQL, a PL/SQL block could consist of nothing more than a single call to a procedure.

Page 8: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Procedure: Syntax

Page 9: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Cont .

Page 10: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Syntax

CREATE [OR REPLACE] PROCEDURE procedure name

[(parameter [{IN | OUT | IN OUT}] type,....,

parameter [{IN | OUT | IN OUT}] type)] AS

[local_variable_declarations]

BEGIN

procedure_body;

END procedure name;

Page 11: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Procedure

A procedure is called as an executable PL/SQL statement. In other words, a call to a procedure must end with a semicolon (;) and be executed before and after other SQL or PL/SQL statements (if they exist) in the execution section of a PL/SQL block.

The following executable statement runs the apply_discount procedure:

BEGIN apply_discount( new_company_id, 0.15 ); -- 15% discountEND;

Page 12: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Procedure

If the procedure does not have any parameters, then you call the procedure without any parentheses:

display_store_summary; In Oracle8i Database and later, you can also include empty open

and close parentheses as well, as in:display_store_summary(); A programmer does not need to know about the inside of the

procedure (the body)to be able to call it properly from another program.

The header for the apply_discount procedure mentioned in the previous section is:

PROCEDURE apply_discount(company_id_in IN company.company_id%TYPE, discount_in IN

NUMBER)It consists of the module type, the name, and a list of two

parameters.

Page 13: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous
Page 14: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Procedure

The Procedure Header is The portion of the procedure definition that comes before the IS keyword.

The header provides all the information a programmer needs to call that procedure, namely:• The procedure name.• The parameter list, if any.

Page 15: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Procedure

In the Procedure Header, The word REPLACE is optional.

When REPLACE is not used in the header of the procedure, to change the code in the procedure, you must drop and then re-create the procedure. Because it is very common to change a procedure’s code, especially when it is under development.

it is strongly recommended that you use the OR REPLACE option.

Page 16: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Procedure

The Procedure Body is the code required to implement that procedure, and consists of the declaration, execution, and exception sections of the procedure.

Everything after the IS keyword in the procedure makes up that procedure’s body.

The exception and declaration sections are optional. If you have no exception handlers, leave off the EXCEPTION keyword and simply enter the END statement to terminate the procedure.

If you have no declarations, the BEGIN statement simply follows immediately after the IS keyword.

You must supply at least one executable statement in a procedure.

Page 17: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Procedure

The END Descriptor, You can append the name of the procedure directly after the END keyword when you complete your procedure, as shown here:

PROCEDURE display_stores (region_in IN VARCHAR2) ISBEGIN...END display_stores;

This name serves as a label that explicitly links the end of the program with its beginning.

You should, as a matter of habit, use an END descriptor.

Page 18: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Procedure: Example

Page 19: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Invoking (Calling) the Procedure

Page 20: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Stored procedure parametersWe can pass parameters to

procedures in three ways:1) IN-parameters.2) OUT-parameters.3) IN OUT-parameters.

Page 21: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Stored procedure

CREATE PROCEDURE HELLO ISBEGINDBMS_OUTPUT.PUT_LINE(’Hello World’);END;

The above declares a HELLO procedure that just displays ’Hello World’.

You can run it as part of a code fragment, or inside other procedures (or functions).

For example:BEGINHELLO();END;

Or you can simply execute it in SQL*Plus by typing:CALL HELLO();

Page 22: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Stored procedure

Would setup some procedure to accept an INT variable named N. Writing a simple procedure to display a variable name, you can come up with something like this:

CREATE PROCEDURE DISPN (N INT) ISBEGINDBMS_OUTPUT.PUT_LINE(’N is ’ || N);END;

Which if you call, will promptly display:

SQL> CALL DISPN(1234567891);N is 1234567891

ORbegin

DISPN(1234567891);end;

/

Page 23: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Stored procedure

You can also have multiple parameters. For example, you can accept A and B and display their sum and product.

CREATE OR REPLACEPROCEDURE DISP_AB (A INT, B INT) ISBEGINDBMS_OUTPUT.PUT_LINE(’A + B = ’ || (A + B));DBMS_OUTPUT.PUT_LINE(’A * B = ’ || (A * B));END;

Which when ran, displays something like (depending on the values you provide):SQL> CALL DISP_AB(17,23);A + B = 40A * B = 391

Page 24: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Stored procedure

it should be noted that you can use any PL/SQL type as an argument. For example, VARCHAR2 and others are perfectly acceptable.

For example:CREATE OR REPLACE

PROCEDURE DISP_NAME (NAME VARCHAR2) ISBEGINDBMS_OUTPUT.PUT_LINE(’Hi ’ || NAME || ’!’);END;Which when called displays:SQL> CALL DISP_NAME(’Amal’);Hi Amal!

Page 25: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Stored procedure

We’ve only been giving the procedure data via parameters. This is the default (IN).

What we could also do is get data from the procedure, via an OUT parameter. To do that, we simply specify OUT in between the parameter name and its type. For example:CREATE OR REPLACEPROCEDURE SUM_AB (A INT, B INT, C OUT INT) ISBEGINC := A + B;END; Notice that the above code does not display the resulting sum, it

just changes the value of the C parameter. Also notice the word OUT right after the declaration of C parametername.

Page 26: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Cont,

we will use a code fragment to call the procedure:DECLARER INT;BEGINSUM_AB(23,29,R);DBMS_OUTPUT.PUT_LINE(’SUM IS: ’ || R);END;

Which when ran, displays:

SUM IS: 52

Page 27: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Example ( IN OUT )

CREATE OR REPLACE PROCEDURE squareNum(x IN OUT number) IS BEGIN

x := x * x; END squareNum;

DECLARE a number;

BEGIN a:= 23; squareNum(a); dbms_output.put_line(' Square of (23): ' || a);

END; /

Page 28: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Dropping Procedures

If you’re interested in getting rid of a procedure totally, you can DROP it. The general format of a DROP is:

DROP PROCEDURE procedure_name;

Page 29: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Function

Page 30: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Function

A function is a module that returns a value. Unlike a procedure call, which is a standalone executable statement, a call to a function can exist only as part of an executable statement, such as an element in an expression or the value assigned as the default in a declaration of a variable.

Because a function returns a value, it is said to have a data type. A function can be used in place of an expression in a PL/SQL statement having the same data type as the function.

Page 31: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Function: Syntax

Page 32: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous
Page 33: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

The Function Header

The function header : is the portion of the function definition that comes before the IS keyword. The header provides all the information a

programmer needs to call that function, namely:• The function name.• The parameter list, if any.• The RETURN datatype.

A programmer should not need to look at the inside of the function (its body) in order to be able to call it properly from another program.

Page 34: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

The Function Body

The body of the function: is the code required to implement the function.

It consists of the declaration, execution, and exception sections of the function. Everything after the IS keyword in the function makes up that function’s body.

Once again, the declaration and exception sections are optional. If you have no exception handlers, simply leave off the

EXCEPTION keyword and enter the END statement to terminate the function.

If you have no declarations, the BEGIN statement simply follows immediately after the IS keyword.

A function’s execution section should have a RETURN statement in it.

If, however, your function finishes executing without processing a RETURN statement, Oracle will raise the following error :

ORA-06503: PL/SQL: Function returned without value.

Page 35: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

The RETURN Statement

A function must have at least one RETURN statement in its execution section of statements.

It can have more than one RETURN, but only one is executed each time the function is called.

The RETURN statement that is executed by the function determines the value that is returned by that function.

When a RETURN statement is processed, the function terminates immediately and returns control to the calling PL/SQL block.

The RETURN clause in the header of the function is different from the RETURN statement in the execution section of the body.

While the RETURN clause indicates the datatype of the return or result value of the function, the RETURN statement specifies the actual value that is returned.

You have to specify the RETURN datatype in the header, but then also include at least one RETURN statement in the function.

The datatype indicated in the RETURN clause in the header must be compatible with the datatype of the returned expression in the RETURN statement.

Page 36: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

The END Descriptor

You can append the name of the function directly after the END keyword when you complete your function, as shown here:

FUNCTION tot_sales (company_in IN INTEGER) RETURN NUMBERISBEGIN...END tot_sales;

This name serves as a label that explicitly links the end of the program with its beginning.

You should, as a matter of habit, use an END descriptor.

Page 37: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Calling a Function

A function is called as part of an executable PL/SQL statement wherever an expression can be used .

Page 38: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Assign the default value of a variable

• Assign the default value of a variable with a function call:

DECLAREv_nickname VARCHAR2(100) :=favorite_nickname ('Steven');

Page 39: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Functions Without Parameters

If a function has no parameters, the function call is written without parentheses.

Page 40: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

The RETURN Datatype

A PL/SQL function can return virtually any kind of data known to PL/SQL, from scalars (single, primitive values like dates and strings) to complex structures such as collections, object types, cursor variables, and LOBs.

You may not, however, return an exception through a function, because in PL/SQL, exceptions do not have a type.

Page 41: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Function: Example

Page 42: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Invoking the Function

Page 43: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Passing Parameter to the Function

Page 44: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Invoking the Function with a Parameter

Page 45: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Example procedure

CREATE OR REPLACE PROCEDURE greetingsASBEGIN dbms_output.put_line('Hello World!');END;/-----------------------------------------------BEGIN greetings;END;/

Page 46: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Example Function

CREATE or replace function Sumnum (N number, M number) RETURN numberISBEGINRETURN N+M;END Sumnum;/-----------------------------------------------

set serveroutput onBEGIndbms_output.put_line('The sum is '||Sumnum(2,2));END ;/

Page 47: CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous

Summary

In this lecture, you should have learned how to:

• Create a simple procedure• Invoke the procedure from an anonymous block• Create a simple function• Create a simple function that accepts parameters• Invoke the function from an anonymous block