Creating a Stored Procedure or Function in an Oracle Database

A stored procedure or function can be created with no parameters, IN parameters, OUT parameters, or IN/OUT parameters. There can be many parameters per stored procedure or function.

An IN parameter is a parameter whose value is passed into a stored procedure/function module. The value of an IN parameter is a constant; it can't be changed or reassigned within the module.

An OUT parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module. You cannot assign a default value to an OUT parameter outside of the module's body. In other words, an OUT parameter behaves like an uninitialized variable.

An IN/OUT parameter is a parameter that functions as an IN or an OUT parameter or both. The value of the IN/OUT parameter is passed into the stored procedure/function and a new value can be assigned to the parameter and passed out of the module. An IN/OUT parameter must be a variable, not a constant. However, it can be found on both sides of an assignment. In other words, an IN/OUT parameter behaves like an initialized variable.

This example creates stored procedures and functions demonstrating each type of parameter.

See also Calling a Stored Procedure in a Database and Calling a Function in a Database.

try { // To create a connection to an Oracle database, // see Connecting to an Oracle Database Statement stmt = connection.createStatement(); // Create procedure myproc with no parameters String procedure = "CREATE OR REPLACE PROCEDURE myproc IS " + "BEGIN " + "INSERT INTO oracle_table VALUES('string 1'); " + "END;"; stmt.executeUpdate(procedure); // Create procedure myprocin with an IN parameter named x. // IN is the default mode for parameter, so both `x VARCHAR' and `x IN VARCHAR' are valid procedure = "CREATE OR REPLACE PROCEDURE myprocin(x VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES(x); " + "END;"; stmt.executeUpdate(procedure); // Create procedure myprocout with an OUT parameter named x procedure = "CREATE OR REPLACE PROCEDURE myprocout(x OUT VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES('string 2'); " + "x := 'outvalue'; " // Assign a value to x + "END;"; stmt.executeUpdate(procedure); // Create procedure myprocinout with an IN/OUT parameter named x; // x functions as an IN parameter and also as an OUT parameter procedure = "CREATE OR REPLACE PROCEDURE myprocinout(x IN OUT VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES(x); " // Use x as IN parameter + "x := 'outvalue'; " // Use x as OUT parameter + "END;"; stmt.executeUpdate(procedure); // Create a function named myfunc which returns a VARCHAR value; // the function has no parameter String function = "CREATE OR REPLACE FUNCTION myfunc RETURN VARCHAR IS " + "BEGIN " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); // Create a function named myfuncin which returns a VARCHAR value; // the function has an IN parameter named x function = "CREATE OR REPLACE FUNCTION myfuncin(x VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "RETURN 'a return string'||x; " + "END;"; stmt.executeUpdate(function); // Create a function named myfuncout which returns a VARCHAR value; // the function has an OUT parameter named x whose value is // returned to the calling PL/SQL block when the execution of the function ends function = "CREATE OR REPLACE FUNCTION myfuncout(x OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "x:= 'outvalue'; " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); // Create a function named myfuncinout that returns a VARCHAR value; // the function has an IN/OUT parameter named x. As an IN parameter, the value of x is // defined in the calling PL/SQL block before it is passed in eyfuncinout // function. As an OUT parameter, the new value of x, `x value||outvalue', is also // returned to the calling PL/SQL block when the execution of the function ends. function = "CREATE OR REPLACE FUNCTION myfuncinout(x IN OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "x:= x||'outvalue'; " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); } catch (SQLException e) { }

Comments

15 Mar 2010 - 2:58pm by emake (not verified)

am getting started

7 Jun 2010 - 9:40am by Anonymous (not verified)

Well done...but, i guess...is so simple compile store procedures directly in Oracle database...put this code in java classes seems so difficult and makes class not easy to reuse...

I guess...repeat..sorry for my english...
Nevermind, congrats!!!, nice post...

6 Aug 2010 - 2:31am by suraj (not verified)

This is nice .........

19 Aug 2010 - 1:53am by Anonymous (not verified)

Nice one !!!

3 Sep 2010 - 2:51am by frank (not verified)

good

6 Sep 2010 - 8:17pm by Cheap UGG Boots (not verified)

Cheap UGG Boots On Sale!We are the best ugg boots saler in UK.Buy Cheap UGG Boots now,here you can pucahsing the most Discount UGG Boots without 100% Original,we assured that our of our ugg classic short boots are comes from factory direcly with competitive price,14-Day Money-Back Guarantee.Fast And Free Shipping! Save 20-60% OFF!
UGG Amelie Suede
UGG Bailey Button
UGG Classic Cardy
UGG Classic Crochet
Discount UGG Classic Mini
UGG Classic Short
UGG Classic Tall
UGG Coquette
UGG Gypsy Sandal
UGG Hammond Slippes
UGG Infant's Erin
UGG Knightsbridge
UGG Lo Pro Button
UGG Nightfall
UGG Slippers
UGG Sundance
UGG Ultra Short
UGG Ultra Tall

6 Sep 2010 - 8:19pm by Cheap UGG Boots (not verified)

Do you want to buy Cheap Women's Ugg Bailey Button?Softugg.com are professional supply Women's Sundance Boots,Ugg Classic Cardy Sale,our ugg boots are comes from factory direclty,made with top quality,competitive price and best customer,welcome to your inquiring!

7 Sep 2010 - 10:17am by lfclhy13709394 (not verified)

swimming hat adorned with green and white daisies and a 1940s sun hat, complete with matching bag, replica omega seamaster watches
made in ChicagoThe sale, taking place tomorrow at Thomas Mawer And Son auction house replica tag heuer watches
in Portland Street, also includes 1950s day dresses, Victorian mourning costumes and 1970s platform shoesMost vpatek philippe replica watches
Popular ArticlesAznar, Trimble to launch new pro-Israel project Too stressfulWiden it to help ease traffic jamWar of words on nukes continues"This sale will tap in replica tag heuer watches
to that demand, indulging the passion

8 Sep 2010 - 1:06am by UGG Classic Tall (not verified)

Spring is a season full of UGG Classic Short vitality and youth, it seems that UGG Ultra Short everyone wants to fully prove themselves this season so comfortable. Just as the designers hope ugg, customers can glory shining in the spring.
First, you can use a slightly relaxed jeans indigo wash, which can make it as UGG Classic Mini real and easily. And the black knitwear UGG Classic Cardy can make it much cooler. Also, you can take a thick strawberry carry Ugg boots. Moreover, you can wear jeans ugg boots.
In my opinion, girls like clothes, jewelry, or things UGG Classic Tall like that. The best post-sale: Hurlling hockey stick So I chose a beautiful pin for her. Leighton Meester starting point you to keep photo albums latest price Although it can be very valuable, I thought I could make my cousin happy. At the same time, I was UGG Handbags very curious about what my parents sent him.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.