Thursday, May 17, 2012

Basic Oracle Stored Procedure Structure

Previously
For a basic Oracle function, visit my article Basic Oracle Function Structure. For a more advanced version which uses cursors to work with SSRS, see my article Oracle Stored Procedures in SSRS.

What?
I asked someone for a simple, easy and basic Oracle stored procedure as my MySQL and T-SQL stored procedures work slightly differently and are easier to pull off. After much umming and aah-ing, I have written this article as a note for me to demo a working stored procedure and how to use cursors. I'm told I need to use this for SQL Server Reporting Services (SSRS) which is why I'm going down this route. For an even simpler stored procedure, I'd imagine you just get rid of the cursor.

Why
A standalone query is faster in the eyes of SSRS, however within SSRS we don't have the capability to run standard PL/SQL commands... unless they're run from within a stored procedure. Our aim is to run a small query first to return the ID of the student, and then to use this number in a second query which we're hoping will be quicker than a straightforward table join.

Syntax
CREATE [OR REPLACE] PROCEDURE stored_procedure_name 
    [ (parameter [,parameter]) ]
IS | AS
    [declaration_section]

BEGIN
    executable_section

[EXCEPTION
    exception_section]

END [stored_procedure_name ];

Example
CREATE OR REPLACE PROCEDURE sp_get_studentdetails_from_ad (
	c_test out sys_refcursor, 
	p_STUDENT_ADNAME in varchar2
	) 
IS
BEGIN
   OPEN c_test FOR 
   SELECT * FROM student_accounts WHERE student_username = p_STUDENT_ADNAME;
END;

View the results?
Oracle PL/SQL is a bit of a bugger as you need to do a few other things to see the results of this stored procedure. The following are commands that I would run to view the results of the above example:
VARIABLE myResultSet REFCURSOR;
EXEC sp_get_studentdetails_from_ad(:myResultSet,'jsmith2012');
PRINT myResultSet;
Should yield something like:
MYRESULTSET
--------------------------------------------------------------------------------
STUDENTID  STUDENT_USERNAME  STUDENT_CREATION_DATE  STUDENT_FILE              
---------- ----------------- ---------------------- ----------------------------
1234567    jsmith2012        08-MAY-12              sac180786.txt                

Permissions
CREATE PUBLIC SYNONYM sp_get_studentdetails_from_ad FOR base_table_owner.sp_get_studentdetails_from_ad;
GRANT EXECUTE ON sp_get_studentdetails_from_ad TO a_more_casual_user;

Yay for me
So this is the basic Oracle stored procedure. Now to integrate this with SQL Server Reporting Services (SSRS) v2008 R2...

Add comment

Please note: all comments are reviewed before being published.


Security code
Refresh

Member Login

Joes Latest Members

A huge WELCOME goes to today's newest members:

  • infonat
Member Signups (Activated)
BeforeCurrentTrend
Day21=
Week105ê
Month11224ê
Year2,265612ê

Member Stats
53 guests are currently online.
1,178 members are still deciding.
There are 5,935 members in total.

Latest Comments

Paypal Donations

Want to support my work? Any donation is a blessing :c)