Tuesday, July 5, 2011

Welcome to the world of Stored Procedures- Oracle, MS SQL,MySQL


Welcome to Stored Procedures
A stored procedure is a precompiled group of Transact-SQL statements, and is saved to the database .Programmers and administrators can execute stored procedures either from the SQL Server Management Studio or from within an application as required.
Once the stored procedure has been "stored", client applications can execute the stored procedure over and over again without sending it to the database server again and without compiling it again.
Stored procedures improve performance by reducing network traffic and CPU load.



Benefit
Explanation of benefit
Modular programming
You can write a stored procedure once, then call it from multiple places in your application.
Performance
Stored procedures provide faster code execution and reduce network traffic.
  • Faster execution: Stored procedures are parsed and optimized as soon as they are created and the stored procedure is stored in memory. This means that it will execute a lot faster than sending many lines of SQL code from your application to the SQL Server. Doing that requires SQL Server to compile and optimze your SQL code every time it runs.
  • Reduced network traffic: If you send many lines of SQL code over the network to your SQL Server, this will impact on network performance. This is especially true if you have hundreds of lines of SQL code and/or you have lots of activity on your application. Running the code on the SQL Server (as a stored procedure) eliminates the need to send this code over the network. The only network traffic will be the parameters supplied and the results of any query.
Security
Users can execute a stored procedure without needing to execute any of the statements directly. Therefore, a stored procedure can provide advanced database functionality for users who wouldn't normally have access to these tasks, but this functionality is made available in a tightly controlled way.


 You can find detailed stored procedures of popular database systems here:
a. Oracle  : http://codingbasics.blogspot.com/2011/07/stored-procedures-in-ms-sql.html
b. MSSQL : http://codingbasics.blogspot.com/2011/07/stored-procedures-in-oracle.html
c. MySQL : http://codingbasics.blogspot.com/2011/07/stored-procedures-in-mysql.html

Stored Procedures in MS-SQL

Creating a Stored Procedure
You create stored procedures in the SQL Server Management Studio using the CREATE PROCEDURE statement, followed by the code that makes up the stored procedure.
CREATE PROCEDURE StoredProcedureName AS
...
The following code creates a stored procedure called "MyStoredProcedure":
CREATE PROCEDURE MyStoredProcedure AS
SET ROWCOUNT 10
SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
FROM Products
ORDER BY Products.UnitPrice DESC
Once you run this code in the SQL Server Management Studio, the stored procedure is created and appears under the "Stored Procedures" node.

Modifying a Stored Procedure

If you need to modify an existing stored procedure, you simply replace the CREATE with ALTER.
ALTER PROCEDURE MyStoredProcedure AS
...
more

Stored Procedures in MySQL


Working with Stored Procedures in MySQL

Writing the first stored procedure
The first stored procedure is very simple. It retrieves all products from products table. First let’s take a look at the stored procedure source code below:

1
DELIMITER //
2
CREATE PROCEDURE GetAllProducts()

3
  BEGIN
4
  SELECT *  FROM products;

5
  END //
6
DELIMITER ;
The first command you see is DELIMITER //. This command is not related to the stored procedure. DELIMITER statement in MySQL is used to change the standard delimiter (semicolon) to another. In this case, the delimiter is changed from semicolon(;) to //, so you can have multiple SQL statements inside stored procedure which can be separated by the semicolon. After the END keyword we use delimiter // to show the end of the stored procedure. The last command changes the delimiter back to the standard one (semicolon).
more

Stored Procedures in Oracle


Working with Stored Procedures in Oracle


Procedures are written in Oracle's PL/SQL programming language.
Sample Procedure
In Notepad, type:
CREATE OR REPLACE PROCEDURE sample
IS
BEGIN
  NULL;
END;

Save your file as sample.sql.
Let’s go through the procedure line by line. The lines are numbered for your convenience:
1 CREATE OR REPLACE PROCEDURE sample
2 IS
3 BEGIN
4   NULL;
5 END;

Line 1:
CREATE OR REPLACE PROCEDURE is an SQL statement that instructs Oracle to create a procedure called sample, and to overwrite it if it exists.
Line 2:
The IS keyword signals that a PL/SQL body will follow.
Line 3:
The BEGIN keyword signals the start of a PL/SQL body.
Line 4:
The NULL PL/SQL statement indicates that no action should be performed. We cannot just leave it out, because at least one statement is required in a PL/SQL body.
Line 5:
The END keyword signals the end of the PL/SQL block.
Now let's move on to creating a procedure.


Read more

subversion video