Introduction To SQL Stored Procedures
Introduction To SQL Stored Procedures
Introduction To SQL Stored Procedures
Summary: in this tutorial, you will learn what a stored procedure is, its advantages and disadvantages. Definition of stored procedure A stored procedure is a segment of declarative SQL code, which is stored in the database catalog. A stored procedure can be invoked by a program, a trigger or even another stored procedure. A stored procedure which calls itself is known as recursive stored procedure. Almost RDMBS supports recursive stored procedure but MySQL does not support it very well. You should check your version of MySQL database before implementing recursive stored procedures. Stored Procedure in MySQL MySQL is known as the most popular open source RDBMS which is widely used by both community and enterprise. However during the first decade of its existence, it did not support stored procedure, trigger, eventetc. Since MySQL version 5.0, those features have been added to MySQL database engine to make it become flexible and powerful. Before starting the tutorial series about stored procedure, you should have MySQL version 5.x+ installed in your computer or server. Stored Procedures Advantages Stored procedures help increase the performance of application using them. Once created, stored procedures are compiled and stored in the database catalog. A stored procedure typically runs faster than uncompiled SQL statements that are sent from external applications. Stored procedures reduce the traffic between application and database server because instead of sending multiple uncompiled lengthy SQL statements, the application only has to send the stored procedures name with parameters. Stored procedures are reusable and transparent to any applications. Stored procedures expose the database interface to all applications so developers dont have to develop functions that are already supported in stored procedure in those applications. Stored procedures are secured. Database administrator can grant appropriate permission to application that access stored procedures in database catalog without giving any permission on the underlying database tables. Besides those advantages, stored procedures have their own disadvantages, which you should be aware of before deciding using store procedures. Stored Procedures Disadvantages Stored procedures make the database server high load in both memory and processors. Instead of being focused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations which is not well-designed for database server.
Stored procedures consist of declarative SQL so it is very difficult to write a procedure with complexity of business logic like other languages in application layer such as Java, C#, C++ Stored procedures are difficult to debug. Only few database mangement system supports debugging stored procedures. There are some workarounds on this problem but it still not easy enough. Store procedures are not easy to develop and maintain. Developing and maintaining stored procedure are usually required specialized skill set that not all application developers possess. This may introduce problems in both application development and maintain phases. Stored procedure has it own advantages and disadvantages. Therefore when you develop application, you should balance between them to decide whether to use stored procedure or not. In the following tutorials, we will show you how to leverage stored procedure in your database programming tasks with a couple of practical examples.
Stored Procedures What is a Stored Procedure? A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage. We can pass parameters to procedures in three ways. 1) IN-parameters 2) OUT-parameters 3) IN OUT-parameters A procedure may or may not return any value. General Syntax to create a procedure is: CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] IS Declaration section BEGIN Execution section EXCEPTION Exception section
END; IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section. The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE together the procedure is created if no other procedure with the same name exists or the existing procedure is replaced with the current code. The below example creates a procedure employer_details which gives the details of the employee. CREATE OR REPLACE PROCEDURE employer_details IS CURSOR emp_cur IS SELECT first_name, last_name, salary FROM emp_tbl; emp_rec emp_cur%rowtype; BEGIN FOR emp_rec in sales_cur LOOP dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name || ' ' ||emp_cur.salary); END LOOP; END; / How to execute a Stored Procedure? There are two ways to execute a procedure. 1) From the SQL prompt. EXECUTE [or EXEC] procedure_name; 2) Within another procedure simply use the procedure name. procedure_name; NOTE: In the examples given above, we are using backward slash / at the end of the program. This indicates the oracle engine that the PL/SQL program has ended and it can begin processing the statements.