Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Stored Program of My Sql

Stored Program of My Sql



|Views: 967|Likes:
Published by Tanay Sarkar

More info:

Published by: Tanay Sarkar on Dec 20, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Definition of stored procedure
A stored procedure, by definition, is a segment of declarative SQL code which is stored in the databasecatalog and can be invoked later by a program, a trigger or even a stored procedure.A stored procedure, which calls itself, is recursive stored procedure. Almost RDMBS supports recursivestored procedure but MySQL does not support it well. Check your version of MySQL before using recursivestored procedure
Stored procedure in MySQL
MySQL certainly is the most open source RDBMS which is widely used by both community and enterprise but during the first decade of its existence, it did not support stored procedure, trigger, event«SinceMySQL version 5.0, those features has been added to MySQL database engine to allow MySQL moreflexible and powerful. Therefore before start the tutorial series about Stored procedure, it is required thatyou have MySQL version > 5.x installed in your computer.
Stored procedures advantages
Stored procedure increases performance of application. Once created, stored procedure is compiled andstored in the database catalog. It runs faster than uncompiled SQL commands which are sent fromapplication.Stored procedure reduced the traffic between application and database server because instead of sendingmultiple uncompiled long SQL commands statement, application only has to send the stored procedurename and get the result back.Stored procedure is reusable and transparent to any application which wants to use it. Stored procedureexposes the database interface to all applications so developer doesn¶t have to program the functions whichare already supported instored procedure in all programs.Stored procedure is secured. Database administrator can grant the right to application which to access whichstored procedures in database catalog without granting any permission on the underlying database table.Beside those advantages, stored procedure still has its own disadvantages which are bellow
Stored procedures disadvantages
Stored procedure make the database server high load in both memory for and processors. Instead of beingfocused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations or a complex of business logic which is not the role of it.Stored procedure only contains declarative SQL so it is very difficult to write a procedure with complexityof business like other languages in application layer such as Java, C#, C++«You cannot debug stored procedure in almost RDMBSs and in MySQL also. There are some workaroundson this problem but it still not good enough to do so.
riting and maintain stored procedure usually required specialized skill set that not all developers possess.This introduced the problem in both application development and maintain phrase.Stored procedure has it own advantages and disadvantages as mentioned above. So when developingapplication you should balance between them to choose whether to usestored procedure or not. Thefollowing tutorial we will guide you how to leverage stored procedure in your database programming task with a couple of practical examples.In this tutorial, you will write the first simple stored procedure and invoke it from command line of MySQL.
riting the first stored procedure
The first stored procedure is very simple. It retrieves all products from
table. First let¶s take a look at the stored procedure source code bellow:
The first command you see is DELIMITER //. This command is not related to the stored procedure.DELIMITER statement is used to change the standard delimiter (semicolon) to another, in this case thedelimiter is changed to //, so you can have multiple SQL statements insidestored procedure which canseparate 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).In order to create a new stored procedure you use CREATE PROCEDURE statement. After the CREATEPROCEDURE statement you can specify the name of stored procedure, in this case it is
 The body part of the stored procedure started with between BEGIN and END block. You can writedeclarative SQL code here.
e can analysis more details of each part later. Now we have created anewstored procedure, but we also need to know however to invoke it in program or in command line of MySQL.
alling the stored procedure
In order to invoke a stored procedure we use the following SQL command:
For example, we can call the stored procedure we have created like thisCALL GetAllProducts();
e get all products in the
database table.In this tutorial, you¶ve learn how to change the delimiter by using DELIMITER statement. It allows you totype multiple SQL statements insidestored procedure. You¶ve also learn how to write a simple stored
 procedure by using CREATE PROCEDURE statement and call it from command line by using CALLstatement.
Declaring variables
Variables are used in stored procedure to store the immediate result. You can declare a variable by thefollowing syntax:
DECLARE variable_name datatype(size) DEFAULT default_value;
Followed DECLARE statement is the variable name. The variable name should follow the namingconvention and should not be the same name of table or column in a database. Next you can specify the datatype of the variable, it can be any primitive type which MySQL supports such as INT, VARCHAR andDATETIME«along with the data type is the size of the variable.
hen you declare a variable, its initialvalue is NULL. You can also assign the default value for the variable by using DEFAULT statement. For example, we can define a variable name total_sale with thedata type INT and default value is 0 as follows:DECLARE total_sale INT DEFAULT 0To declare two or more variables with the same data type we can use only just one DECLARE such as:
e declared two variables x and y with the same data type INT and their default value is zero.
ssigning variables
Once you declared a variable, you can start using it. To assign other value to a variable you can use SETstatement, for example:
DECLARE total_count INT DEFAULT 0SET total_count = 10;
The total_count variable¶s value now is ten (10).Beside SET statement, we can use SELECT « INTO to assign a query result to a variable.
DECLARE total_products INT DEFAULT 0SELECT COUNT(*) INTO total_productsFROM products
In the example above, we declare a variable total_products and initialize its value to zero. Then we useSELECT « INTO statement to assign the variable total_products with the total products in
 database table.
ariables scope
A variable has its own scope. If you declare a variable inside a stored procedure, it will be out of scopewhen the END of stored procedure reached. If you defined a variable inside block BEGIN/END inside astored procedure it will be out of scope if the END reached. You can declare two variables or more variableswith the same name in different scopes; the variable only is effective in its scope.

Activity (10)

You've already reviewed this. Edit your review.
1 hundred reads
donv123 liked this
sinha.rajeev liked this
chrokoth liked this
cyrenityx liked this
pqtrung5th1 liked this
shekar_bandi liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->