Thứ Năm, 11 tháng 9, 2014

Creating Stored Procedures in MYSQL.

A Stored procedure is a set of SQL statements are executed with a single call.(Something like similar to your c program calls).

Syntax:

CREATE PROCEDURE  your_procedure_name(parameters list...)

parameters list were those you specify while calling your procedure.Mysql procedure accept three type of parameters IN,OUT,INOUT.

IN-means your procedure accepting an input parameter.
OUT-means your procedure sends a value as a parameter to the calling one.
INOUT-means combination of in,out.

example:

First I will create a table called country to demonstrate procedures.

MySQL>create table country(name varchar(100),id int(40));

now insert some values in the above table.

MySQL>insert into country values('INDIA',1);
 
MySQL>insert into country values('USA',2);
 
MySQL>insert into country values('AUS',3);
 
MySQL>insert into country values('PAK',1);
 
MySQL>insert into country values('UK',1);
 
MySQL>insert into country values('BAN',1);  

lets create a  procedure for retrieving names of country with id 1 by crating procedure.

MySQL>delimiter //
MySQL>

CREATE PROCEDURE  country_procedure(IN cid  int(10))
BEGIN
             select name from country where id=cid;
END  //
MySQL>delimiter ;
Now call the procedure....
MySQL>call country_procedure(1);
****************
    INDIA                  
    PAK                     
    UK                       
    BAN                     
*****************


 

Không có nhận xét nào:

Đăng nhận xét