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 ];
- 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;
- 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:
EXEC sp_get_studentdetails_from_ad(:myResultSet,'jsmith2012');
- EXEC sp_get_studentdetails_from_ad(:myResultSet,'jsmith2012');
MYRESULTSET -------------------------------------------------------------------------------- STUDENTID STUDENT_USERNAME STUDENT_CREATION_DATE STUDENT_FILE ---------- ----------------- ---------------------- ---------------------------- 1234567 jsmith2012 08-MAY-12 sac180786.txt
- 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;
- 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...