com/ Raju Ch

1 Stored Procedures in PL/SQL
Many modern databases support a more procedural approach to databases—they allow you
to write procedural code to work with data. Usually, it takes the form of SQL interweaved
with the more familiar IF statements, etc.
Note that this has nothing to do with accessing the database. You can access any database
from virtually any language. What we’re talking about is the code that is executed by the
database server.
While there are many various ‘database’ languages, we will only talk about the primary
two: T-SQL, which is supported by SQL Server and Sybase, and PL/SQL, which is supported
by Oracle.
Many other languages may be supported. For example, Oracle allows you to write stored
procedures and triggers in Java, etc.

Besides plain vanilla SQL, Oracle supports PL/SQL. The PL stands for Procedural Lan-
guage, which means you can have things like IF statements, loops, variables, and other
procedural things along with declarative SQL statements. PL/SQL G
2.1 Variables L
Just as most procedural languages, PL/SQL has some sort of variables. The types of variables
are plain SQL column types that you’re all used to. You can also refer to a type of a particular
column explicitly by specifying the fully qualified column name (tablename.columname)

followed by %TYPE. For example: PRODUCT.PRICE%TYPE.

Similarly, we can refer to a particular row as a single type. Again, you declare it by

referring to a database table directly: PRODUCT%ROWTYPE. This would refer to a single record
stored in the PRODUCT table.
Along with the above mentioned, some common types are: BOOLEAN, DATE, NUMBER, CHAR,
We declare variables of these types similarly to specifying columns in tables. First, we
list the name of the variable, then the type we want it to have. For example, to declare a

Aprice variable of a fixed point NUMBER, we might do something like this:

O 2.2 PL/SQL Program Blocks
PL/SQL programs are structured in blocks and have the following format:


1 Raju Ch


2.2.1 Declare
The declare part is where variable declaration goes. All used variables must be declared in
this section. This is also the place where other more exotic variable types are declared, like
cursors and exceptions.

2.2.2 Begin

This is the part we’re most interested in. This is where the bulk of your programs shall be
placed. Here, you can have IF statements, loops, etc.
2.2.3 Exceptions
The exception section is where we place error handling code. We will talk about it more
depth in subsequent lessons. S
2.2.4 End
The end signifies the end of this program block. L
2.3 Operators
PL/SQL supports several operators to 8 do various things. Table 1 lists some of the more
common operators. S
2.4 Hello WorldP
and a bit Beyond...
Athe PL/SQL hello world example. Before we write it however, there
are a couple ofE
Well, let’s start with
things that need to be setup. First, start “SQL*Plus” (Oracle), login, and
type: L

R gramsWhatattempts
this does is enable you to view output in SQL*Plus window whenever your pro-
O to write some output to the screen.
Now, let’s get on with the show. Type the following into the SQL*Plus window as is:


You’ll notice that it doesn’t run as an average SQL statement. To make it run, you must
type the ’/’ character on a line by itself. And you’ll notice:


For example: . You can think of DBMS_OUTPUT. 4 / Hello World PL/SQL procedure successfully completed.PUT_LINE(SYSDATE).B We’re not done with this simple example yet.PUT_LINE(’Hello World’).PUT_LINE(’The time now is: ’). P 4 END. It writes output to the console. 3 END. We can also modify the DATE format: ∗∗ 8 8 Exponentiation ∗ Multiplication / S Division + PAddition P−− Subtraction A := Negation E = Assignment L Equals Comparison C <> Not Equals Comparison != Not Equals Comparison A > Greater Than Comparison R < Less Than Comparison O >= <= Greater Than or Equal Comparison Less Than or Equal Comparison AN D The obvious AND operation OR The obvious OR operation := Assignment || String Concatenation Table 1: PL/SQL Operators 3 .C SQL> BEGIN T 2 DBMS_OUTPUT. O 3 DBMS_OUTPUT. but it only works for strings (or data that can be implicitly M O converted to a string).PUT_LINE() as sort of a printf() in C language. like access global variables like SYSDATE. S G 5 / The time now is: 31-JUL-02 O L Raju Ch SQL> BEGIN 2 DBMS_OUTPUT.http://oracleapps88. You can also do more complicated things.

O .END) Returns a substring that starts at Raju Ch SQL> BEGIN 2 DBMS_OUTPUT.blogspot. LENGTH(STR) Returns the length of the string. Table 3: More PL/SQL Functions Substring example follows: 4 .C Table 2 lists some of these useful functions. Converts the string to lower case.PUT_LINE(’The time now is: ’ || SYSDATE). SUBSTR(STR. 5 / The time now is: 07/31/2002 2. TO_NUMBER Converts a character string to a number.6 Character String Functions L .B There are a number of functions for handling character string data in PL/SQL.http://oracleapps88. 3 DBMS_OUTPUT.STR2) Looks for STR2 in STR1. For example. 3 END. A R RTRIM(STR) Removes blank spaces from right side of string.’MM/DD/YYYY’)).PUT_LINE(TO_CHAR(SYSDATE. P 4 / A E The time now is: 31-JUL-02 L Note that || was used to concatenate the string ’The time is now: ’ with the C SYSDATE.PUT_LINE(’The time now is: ’). TO_DATE Converts a string to a date. INSTR(STR1.START. O P S Table 2: Some PL/SQL Functions G O 2. these include 8 the easy to use string catenation operator. O UPPER(STR) LOWER(STR) Converts the string to upper case. you can see we’ve used the TO_CHAR function to format the date. T TO_CHAR Converts numbers or dates to character strings. Some of the more useful PL/SQL are listed in Table 3. we could have written our time now is example as: 8 SQL> BEGIN S 2 P DBMS_OUTPUT.5 Type Conversion Functions M From the previous example. 4 END. Raju Ch SQL> SELECT SUBSTR(’HELLO’. L IF A < B THEN DBMS_OUTPUT.7 PL/SQL IF Statement PL/SQL.THEN Structure T The general format of an IF statement is: O P IF condition THEN S program_statements G O END IF.blogspot. from IF statements to WHILE loops. E B := A * 5.B to spend too much time on the obvious.C so that you can see the output. An example program that uses an IF statement is: 8 DECLARE 8 A NUMBER(6). SUBS ---- ELLO 2.ELSE Structure Just as in any programming language that has an IF statement. and know what IF statements are.9 IF . C END IF. S B NUMBER(6). The full structure of an IF statement is thus: 5 . there is also the ELSE clause to the IF statement. R Which produces the expected output of: O Ans: 23 is less than 115 2. P BEGIN P A A := 23. O .8 IF .http://oracleapps88. being a procedural language naturally has lots of flow control constructs.4) FROM DUAL. L Assuming we all know how to program. I’m not going . M Remember to type: SET SERVEROUTPUT ON in SQL*Plus before running any programs. 2.PUT_LINE(’Ans: ’ || A || ’ is less than ’ || B). A END.

Let’s modify our simple example to: DECLARE M A NUMBER(6). C ABCMAX NUMBER(6). Here. BEGIN O . E B NUMBER(6).C A := 23. T B := A / 5. IF A > B THEN IF A > C THEN ABCMAX := A.B condition. ELSE IF B > C THEN 6 .PUT_LINE(’Ans: ’ || A || ’ is greater than ’ || B). END IF.http://oracleapps88.PUT_LINE(’Ans: ’ || A || ’ is less than ’ || B). R B := A / 5. O ELSE P S DBMS_OUTPUT. G END IF. O C := B * 7. again. END. IF A < B THEN Raju Ch IF condition THEN if_condition_is_true_code ELSE if_condition_is_false_code END IF.blogspot. ELSE ABCMAX := C. O L Note that we’ve also modified the B := A * 5 to B := A / 5 in order to test the ELSE . let’s jump right into P an example: DECLARE P A A NUMBER(6).10 IF Statement nesting 8 8 S We can also put IF statements inside other IF statements. B NUMBER(6). L C NUMBER(6). A BEGIN A := 23. 2.

ELSE LGRADE := ’F’.blogspot. M The code looks self explanatory. B. END IF. ELSIF NGRADE > 60 THEN LGRADE := ’D’. O ELSIF NGRADE > 70 THEN LGRADE := ’C’. we can resort to using ELSIF. END.PUT_LINE(’Max of: ’ || A || ’. E ELSIF NGRADE > 85 THEN L LGRADE := ’B+’. ELSIF NGRADE > 65 THEN LGRADE := ’D+’. END IF. END IF. O 2. P P ELSIF NGRADE > 90 THEN A LGRADE := ’A’. and ’ || C || ’ is ’ || ABCMAX). This is an else if equivalent in C (and in Perl it is actually named elsif). and C).B BEGIN 8 8 NGRADE := 82.11 IF ELSIF Structure . L .5. ELSIF NGRADE > 80 THEN C LGRADE := ’B’. 7 . so we won’t spend much time on it.C T When IF and ELSE are not enough. O P Let’s say we wanted to calculate the letter grade given a number grade. ’ || B || ’.com/ Raju Ch ABCMAX := B. DBMS_OUTPUT. we may write a program such as: S DECLARE G NGRADE NUMBER. The code above finds the maximum value (ABCMAX) of three variables (A.http://oracleapps88. A ELSIF NGRADE > 75 THEN R LGRADE := ’C+’. O LGRADE CHAR(2). ELSE ABCMAX := C. IF NGRADE > 95 THEN S LGRADE := ’A+’.

We’ll do more as we learn PL/SQL. 8 . we used a variable named PID inside our INSERT statement. Loops are used to repeat some action multiple times. which are used to CREATE. 8 INSERT INTO product VALUES (PID. So without further interruption. we can run SQL statements inside PL/SQL! Isn’t that amazing? We can’t use all of SQL though.. ALTER.99). END. Which for our particular example number grade produces output: Grade 82.blogspot. here’s a simple example. P COMMIT.199.’tv’.com/ Raju Ch DBMS_OUTPUT. P END.32.C statements.799.5 is B 2. ROLLBACK.B BEGIN PID := 20. 8 S INSERT INTO product VALUES (PID. where we can use procedural language constructs and variables to drive our database SQL code. PL/SQL also has loops. G we’ll insert a new PRODUCT into a simple database. PID := PID + 1. L C Which produces the expected results: A PRODUCT_ID DESCRIPTION R ---------.PUT_LINE(’Grade ’ || NGRADE || ’ is ’ || LGRADE). and transaction control O . I present to you. INSERT. just to name a few. A We can now run a SELECT statement to retrieve the values we’ve inserted: E SELECT * FROM PRODUCT WHERE PRODUCT_ID >= 20. T O P 2. and DROP tables..12.SQL Apparently. L .12 PL/SQL . until some condition is met. SAVEPOINT.1 Simple Example S For right now.16. like COMMIT. DECLARE O PID NUMBER(6). and GRANT privileges. The only limitation seems to be are DDL statements.’vcr’. UPDATE. we can only use DML (Data Manipulation Language) M which includes statements like SELECT. and DELETE. PL/SQL has five looping structures.http://oracleapps88. ------------ 20 tv O 21 vcr Notice that in our example. PL/SQL Loops Just as with IF statements. That’s the real power of PL/SQL. and we shall talk about each one in more depth as we move along. In this example.98).

PUT_LINE(’bI: ’ || I). A DBMS_OUTPUT. An example of such a loop would be: P DECLARE S I NUMBER(6). T Note that we can place various program statements before the exiting IF statement and O Raju Ch 2. 8 I := I + 1. O . where you use break. to terminate the loop. END LOOP. L .13 LOOP . in this case. which gives us great flexibility about when and how the condition is evaluated.blogspot.. P END IF.C This loop is very similar to an infinite loop in C/C++. E END. the EXIT. G BEGIN O I := 1. L C A With the expected output of: R bI: 2 aI: 1 O aI: 2 bI: 3 aI: 3 bI: 4 aI: 4 bI: 5 aI: 5 9 . command takes the form of break. M various_statements END LOOP.. 8 IF I > 5 THEN S P EXIT. EXIT Loop The general format of such a loop is: LOOP various_statements IF condition THEN EXIT.B LOOP DBMS_OUTPUT.http://oracleapps88. END IF.PUT_LINE(’aI: ’ || I).

the loop terminates. O BEGIN L . output first. Also. The idea is that you have a condition which is tested each time through the loop. The general format of such a loop is: WHILE condition LOOP various_statements END LOOP.C LOOP various_statements EXIT WHEN condition. LOOP Loop A 2. and if it’s false. You shouldE run this code yourself. P An example usage of such a loop would be something like this: S DECLARE G I NUMBER(6).http://oracleapps88. it would be very helpful if you trace the above program to ensure that you under- stand how the loop functions and why the results look as they do. the EXIT WHEN loop.14 LOOP . 8 LOOP DBMS_OUTPUT.blogspot.PUT_LINE(’bI: ’ || I). there is a simpler form.15 R Our next loop is the all familiar WHILE loop.. EXIT WHEN Loop To simplify our writing our the IF statement. Raju Ch Note that you should SET SERVEROUTPUT ON. O It works nearly identically though. I := I + 1. I shall not provide the output for the following code..B I := 1. P A END. in order to see the output in SQL*Plus screen.. except now it is in PL/SQL and not in C/C++. T O various_statements END LOOP. S P DBMS_OUTPUT.. It would actually be more helpful if you write out the L then compare it to the actual results. The general format of such a loop is: M O .PUT_LINE(’aI: ’ || I). 8 EXIT WHEN I > 5. and expect you to run it yourself. and CWHILE . END LOOP. 10 .

END. DBMS_OUTPUT. you’d do something like: DECLARE J NUMBER(6).com/ Raju Ch Our typical (as in typical for these class notes) would be: DECLARE I NUMBER(6).C END LOOP. BEGIN I := 1. I := I + 1. R END. 11 . Tracing questions such as these are fair game for quizzes and tests. or even declare it! It is done implicitly for us by the FOR loop. The general format of such a loop is: L FOR countervariable IN startvalue ..blogspot. WHILE I <= 5 LOOP DBMS_OUTPUT. M O DBMS_OUTPUT. You can also use other variables to loop on. P S 2. O Notice that we never actually directly initialize I. endvalue. For example. to loop from J to K. . and are always incremented by one.B LOOP 8 various_statements 8 END LOOP. T Just as with the previous code.http://oracleapps88. example of such a loop P The start and end values An would be: A BEGIN E L FOR I IN 1. and then O run it to see if your trace was correct. S Pmust be integers.5 C LOOP A END LOOP. You should run this code to ensure you understand it.PUT_LINE(’I: ’ || I).. K NUMBER(6).PUT_LINE(’bI: ’ || I). you should try to figure out what the output is.PUT_LINE(’aI: ’ || I).16 FOR Loop G O There is also the traditional numeric FOR loop that’s commonly found in most procedural languages.

Which interestingly enough.http://oracleapps88.17 Cursors Before we move on with our discussion of the next and last loop construct. S FOR I IN 1.PUT_LINE(’BEFORE LOOP I: ’ || I). O 2. 12 . the loop will still use its O . the I in the loop is M a totally different variable. BEGIN I := 7. Even if you have an I variable declared. FOR I IN K. notice that we never actually initialize nor declare I.PUT_LINE(’IN LOOP I: ’ || I). P DBMS_OUTPUT. . 8 END. we must cover the concept of Cursors.PUT_LINE(’I: ’ || I). END LOOP.PUT_LINE(’AFTER LOOP I: ’ || I).5 G O LOOP L DBMS_OUTPUT. One is implicit and the other one is explicit.C own version. Oracle has two major different types of cursors. You can verify that by running this code: DECLARE T O I NUMBER(6).B DBMS_OUTPUT. END Raju Ch BEGIN J := 7...blogspot. K := 2. In fact.J LOOP DBMS_OUTPUT. END. Again. prints8out: S BEFORE LOOP I: 7 P IN LOOP I: 1 P A IN LOOP I: 2 IN LOOP I: 4E IN LOOP I: 3 IN LOOP L I: 5 C AFTER LOOP I: 7 AWhich illustrates that the value of our declared variable I is unchanged by the loop (and R that the loop internally has I declared which is different from our explicitly declared I).

A DBMS_OUTPUT. 8 S DECLARE P P Raju Ch 2. The only catch is that the search criteria must return one and only one result.C AMOUNT PRODUCT. Where SELECT_statement is any select statement (except a more exotic one which con- tains a UNION or MINUS. The general format goes something like this: SELECT selectfields INTO declared_variables FROM table_list WHERE search_criteria.DESCRIPTION%TYPE. If it returns zero. lets say we wanted to get the name and price of some specific product (identified by PRODUCT_ID): M O DECLARE NAME PRODUCT. NoticeL E that the code got a lot smaller since we don’t have to worry about defining every C single variable for retrieval purposes. O DBMS_OUTPUT. END.blogspot. Let’s rewrite the above using that. 8 Note that we used the table’s types.19 O Explicit Cursor Explicit Cursors are cursors that you have to explicitly declare.http://oracleapps88. and which give you a lot more flexibility than the implicit ones. . 13 . For example. an error is generated. which brings up another issue: Now is a pretty good time to illustrate the ROWTYPE type.95 O L Assuming the “keyboard” is in the database and has PRODUCT ID = 4 (and has that . AMOUNT FROM PRODUCT WHERE PRODUCT_ID = 4. T BEGIN SELECT DESCRIPTION. The output A above code is exactly the same as the previous.PUT_LINE(’PRICE OF ’ || NAME || ’ IS ’ || AMOUNT).B price). of the R 2. To declare an explicit cursor. S Which faithfully displays out: G PRICE OF keyboard IS 19.DESCRIPTION || ’ IS ’ || P.18 Implicit Cursor Implicit cursors can be generated every time you do a SELECT statement in PL/SQL.PRICE%TYPE. you have to do it in the DECLARE section. We retrieve a whole row of data at a time. BEGIN P SELECT * INTO P FROM PRODUCT WHERE PRODUCT_ID = 4.PUT_LINE(’PRICE OF ’ || P. P END.PRICE).PRICE INTO NAME. The format looks something like: CURSOR cursorname IS SELECT_statement. or more than one.

14 . O LOOP FETCH PRODUCTCURSOR INTO P.. 8 8 3. C CURSOR PRODUCTCURSOR IS SELECT * FROM PRODUCT. (obviously you have to do that inside the code section.http://oracleapps88. so hang on. DBMS_OUTPUT.B Cursors will be closed automatically once your code exits. 3. we also have to grab the results of the SELECT statement one by M one. between BEGIN and END).. O P 3.3 S LOOP . You do that with a simple: OPEN cursorname. A BEGIN R OPEN PRODUCTCURSOR.DESCRIPTION || ’ IS ’ || P. L .com/ Raju Ch 3 Opening an Explicit Cursor In order to use an explicit cursor. but it’s still a good idea to close them explicitly. . For example: O FETCH cursorname INTO recordvariables. you must open it.1 Fetching Data into an Explicit Cursor Besides opening the cursor. END LOOP. END. CLOSE PRODUCTCURSOR.. We just say: G O CLOSE cursorname. EXIT WHEN Loop (Again) P We can use our standard loops in order to go loop through the results returned by the cursor. P So. EXIT WHEN PRODUCTCURSOR%NOTFOUND.C T We shall do some examples when we learn our cursor loops. let’s move on to our example: A DECLARE E L P PRODUCT%ROWTYPE.. We do that with a FETCH.PRICE).blogspot.2 Closing a Cursor S Closing a cursor is just as easy as opening it.PUT_LINE(’PRICE OF ’ || P.

%ROWCOUNT: ReturnsP %FOUND: Evaluates to TRUE if last FETCH was successful and FALSE otherwise. LOOP Loop A R easier cursor handling. If we did not exit the loop. it opens and closes the cursor for us.95 PRICE OF monitor IS 399.99 PRICE OF quickcam IS 99. we just CLOSE the cursor..99 T O PRICE OF stapler IS 14. First.99 O . . We EXIT the loop if we got no more results (the PRODUCTCURSOR%NOTFOUND condition). and make sure you understand it. We then fall into our standard loop (which we learned about earlier).blogspot. trace it. we declare our P variable which is a ROWTYPE from table PRODUCT. which simply selects everything from the PRODUCT table.99 PRICE OF calculator IS 7. We then declare our CURSOR.98 P PRICE OF harddrive IS 199. and FETCH results from the CURSOR. run it. Let us rewrite our example program (presented earlier) to use this new type of loop: 15 . the %NOTFOUND. A the number of rows that the cursor has already fetched from the E TRUE if this cursor is already open. database.99 S PRICE OF tv IS 199.B Raju Ch Go through the code line by line.. we output product description and product price. Depending on what you have in your PRODUCT table. There are 8 We’ve already seen one of the more important also these: S P %NOTFOUND: Evaluates to TRUE when cursor has no more rows to read. In the end.4 Cursor Attributes 8 cursor attributes. and FALSE otherwise.5 C Cursor FOR . IN . L %ISOPEN: Returns 3. Our code then proceeds to OPEN the cursor.99 PRICE OF speakers IS 9.. It is a for loop that has the general format: FOR variable(s) IN cursorname LOOP various_program_statements END LOOP.99 G O PRICE OF vcr IS 799. FALSE otherwise.. It allows for O check for the end. the results of the code may look similar to this: M PRICE OF mice IS 26.C PRICE OF keyboard IS 19.http://oracleapps88.98 L You should go through the code. and we don’t have to explicitly There is also a special loop structure made specifically for working with cursors.

Also notice that our cursor S individual fields which we’re displaying.6 IntroductionP P to Stored Procedures Just like any other A E procedural language. END LOOP. 16 . or directly from SQL*Plus (or some other client program). C Before you begin to write procedures though. CURSOR PRODUCTCURSOR IS SELECT * FROM PRODUCT. but just two loop purposes.PRICE FROM PRODUCT. the user someusername will be allowed to create.running Oracle on your own then you need to login as administrator (or ask the administrator) to grant you access.DESCRIPTION || ’ IS ’ || P. R grant such priviledge yourself (in case you’re the administrator .PUT_LINE(’PRICE OF ’ || P. BEGIN FOR P IN PRODUCTCURSOR LOOP DBMS_OUTPUT.PUT_LINE(’PRICE OF ’ || P.blogspot. for example: T DECLARE O CURSOR PRODUCTCURSOR IS P SELECT DESCRIPTION. and replace procedures and functions. 3. PL/SQL has code fragments that are called PROCEDURES. END. you need to verify that you have enough A privileges to do that. If you’re really into optimization. drop. B Notice several things about the code:8 that we no longer declare P which is used for 8 is no longer returning everything. From that point on.DESCRIPTION || ’ IS ’ || P. To O machine) you can do: GRANT CREATE PROCEDURE TO someusername. which O . END LOOP. If you don’t (which probably means you’re using a plain user account). with lots of cursor handling code gone. END. .C is now being handled by the loop itself. M Notice that the code got quite a bit simpler. S BEGIN G O FOR P IN PRODUCTCURSOR LOOP L DBMS_OUTPUT. you might want to improve the above code not to return the whole %ROWTYPE but individual fields which we’re displaying. Raju Ch DECLARE P PRODUCT%ROWTYPE.PRICE). You can call L these PROCEDURES from other code fragments.PRICE).

or inside other procedures (or functions).B HELLO().8 E General Format L The general format of a create procedure statement is this: C A CREATE OR REPLACE R PROCEDURE procedure_name ( parameters ) IS O BEGIN procedure_body END. so you’d want to M add on CREATE (OR REPLACE) to the declaration. parameters is a list of parameters to this procedure (we’ll discuss them later). Raju Ch 3. You can run L it as part of a code fragment.7 PROCEDURES Procedures are code fragments that don’t normally return a value. For example: BEGIN . 8 S Or you can simply execute it in SQL*Plus by typing: P CALL HELLO().C procedure.PUT_LINE(’Hello World’). 8 END. For example. you might do something like this: CREATE OR REPLACE T PROCEDURE HELLO IS O P BEGIN S DBMS_OUTPUT. you’ll usually be either creating or replacing the procedure. to create (or replace) a HELLO O . Of course. Where procedure_name can be any valid SQL name.blogspot.http://oracleapps88. G O The above declares a HELLO procedure that just displays ’Hello World’. The general format of a procedure is: PROCEDURE procedure_name IS BEGIN procedure_body END. and procedure_body is various PL/SQL statements that make up the logic of the procedure. 17 . but may have some outside effects (like updating tables). P A 3.

For example.blogspot.1 Parameters The parameters (or arguments) are optional. For example.C any parameters and doesn’t return anything? Well. like in our case. you may be interested in the procedure’s side effects. which is similar to how variables are declared.PUT_LINE(’A * B = ’ || (A * B)). A E L call. There may be many instances where you may want to just do something to T the database. DBMS_OUTPUT. and without returning anything. a sample procedure. Never actually defines any parameters.PUT_LINE(’Hello World!’). CREATE OR REPLACE PROCEDURE DISP_AB (A INT. Writing a simple pro- 8 cedure to display a variable name. You first specify the name of the variable. this section is about parameters so let’s talk about parameters. O P Anyway. You don’t have to specify anything (not even the parenthesis).com/ Raju Ch 3. S Parameters are defined in a similar way as in a CREATE TABLE statement. M END.PUT_LINE(’A + B = ’ || (A + B)). A N is 1234567891 R O You can also have multiple parameters. will promptly display: Which if you SQL>CCALL DISPN(1234567891). 18 . END. For example: G O (N INT) L .B Would setup some procedure to accept an INT variable named N.http://oracleapps88. and then the type. you can accept A and B and display their sum and product. you can come up with something like this: 8 S CREATE OR REPLACE P PROCEDURE DISPN (N INT) IS BEGIN P DBMS_OUTPUT.PUT_LINE(’N is ’ || N). END. B INT) IS BEGIN DBMS_OUTPUT. we’re interested in our procedure displaying ’Hello World!’ and nothing else. without any particular parameters. What’s the use of a procedure that doesn’t take O .8. which you no doubt have already seen: CREATE OR REPLACE PROCEDURE HELLOWORLD IS BEGIN DBMS_OUTPUT.

A Raju Ch Which when ran. Also notice the word OUT right after the declaration of C parameter Notice that the above code does not display the resulting sum.parameters. 8 What we could also do is get data from the procedure. OUT. For example: CREATE OR REPLACE M O PROCEDURE DISP_NAME (NAME VARCHAR) IS BEGIN DBMS_OUTPUT. VARCHAR and others are perfectly acceptable.29. R Anyway.C T END. it just changes the value A of the name. To do that. Which when ran. For example. BEGIN SUM_AB(23. For example. END.PUT_LINE(’Hi ’ || NAME || ’!’). via an OUT parameter. E L CC parameter. for the time being.http://oracleapps88.PUT_LINE(’SUM IS: ’ || R). 8 we simply specify OUT in between the parameter name and its type. For example: S PROCEDURE SUM_AB (A INT. we will use a code fragment to call the procedure: O DECLARE R INT. Which when called displays: O P SQL> CALL DISP_NAME(’John Doe’).23). DBMS_OUTPUT. There are various different parameter varieties (not types). we’ve only been giving the procedure data via.blogspot. IN OUT O L B This is the default (IN).P CREATE OR REPLACE P B INT. C OUT INT) IS BEGIN C := A + B. A + B = 40 A * B = 391 Btw. displays: 19 . it should be noted that you can use any PL/SQL type as an argument. displays something like (depending on the values you provide): SQL> CALL DISP_AB(17.9 IN. .R). S G Hi John Doe! 3.

There is also the other special way of passing parameters: IN OUT. we also create a small code fragment: T O DECLARE P R INT. END. 20 .B DBMS_OUTPUT. etc. that’s all there is to them. format of a DROP is: The general DROP PROCEDURE procedure_name. To run it. For example. 8 Which when ran displays: 8 S BEFORE CALL R IS: 7 P P AFTER CALL R IS: 14 A Notice how this particular call first grabbed the value of a parameter. we can write a procedure that doubles a number: CREATE OR REPLACE PROCEDURE DOUBLEN (N IN OUT INT) IS M O BEGIN .http://oracleapps88. then we can change it.PUT_LINE(’AFTER CALL R IS: ’ || R). DBMS_OUTPUT. You can use these to setup return values from procedures. We will do some practice exercises and more experimentation.10 Procedures O interested in getting rid of a procedure totally. What that means is that we first can read the parameter. you can DROP it.PUT_LINE(’BEFORE CALL R IS: ’ || R). S G BEGIN R := 7.blogspot. A R If you’reDropping 3. but overall. END. L You can generally intermix these various ways of passing parameters (along with various C types). L . That’s all there is to stored procedures.C N := N * Raju Ch SUM IS: 52 Notice how we called the procedure with an argument to eventually retrieve the OUT result. O DOUBLEN(R). then set it in order E to return the double of the value.

It is a very shady question of when to use what.12 General Format M The general format of a function is very similar to the general format of a procedure: O CREATE OR REPLACE .blogspot. 21 .com/ Raju Ch 3. either functions or procedures.B INT) RETURN INT IS BEGIN 8 RETURN (A + B). A good rule of thumb is: if you’re interested in the “results” of the code. etc. That’s really all there is to it. Usually it doesn’t affect your code all that much if you use a procedure or a function. 3. If you’re interested in the “side effects” (like table updates.C FUNCTION function_name (function_params) RETURN return_type IS T BEGIN O function_body P S RETURN something_of_return_type. and return those results.11 Functions Functions are special types of procedures that have the capability to return a value. to write a function that computes the sum of two numbers. A Which procudes the output: R RESULT IS: 46 O All of a sudden.http://oracleapps88.) and not about the ”result” when you should use a procedure. then you use a function. S P END. we’ll writeP a small piece of code that calls this: A BEGIN E L DBMS_OUTPUT. we know how to make functions (since we already know how to crate procedures). C END.34)). END. you might do something like this: L CREATE OR REPLACE . To run it.PUT_LINE(’RESULT IS: ’ || ADD_TWO(12.B 8 FUNCTION ADD_TWO (A INT. G O For example.

to display the list of procedures/functions or plain general user objects that you have you can run a query: SELECT OBJECT_NAME FROM USER_OBJECTS M WHERE OBJECT_TYPE = ’FUNCTION’. you do it in a similar way to a procedure.C T O P S G O L .blogspot. btw. Oh. O You can do a similar thing for procedures.http://oracleapps88.13 Dropping Functions To drop a function. You simply say: DROP FUNCTION function_name. .com/ Raju Ch 3.B 8 8 S P P A E L C A R O 22 .