Simple use of MySQL stored procedures

Source: Internet
Author: User

First create a students table

The SQL script is as follows:

Create TableStudentsID int Primary KeyAuto_increment, ageint, namevarchar( -), Cityvarchar( -))character SetUTF8;Insert  intoStudentsValues(NULL, A,' Lisa ',' Hangzhou ');Insert  intoStudentsValues(NULL, -,' Rock ',' Shanghai ');Insert  intoStudentsValues(NULL, -,' Jack ',' Shenzhen ');Insert  intoStudentsValues(NULL, +,' Rose ',' Beijing ');

Stored procedures with no parameters
-- 查询学生个数dropprocedureifexists-- 替换分隔符    createprocedure select_students_count()         begin             selectcount(idfrom students;         end ;;delimiter ;

To execute a stored procedure:

call select_students_count();

Stored procedure with parameters
-- 根据城市查询总数delimiter ;;    createprocedure select_students_by_city_count(invarchar(255))        begin            selectcount(idfromwhere city = _city;        end;;delimiter ;

To execute a stored procedure:

call select_students_by_city_count('上海');

Stored procedure with output parameters

MySQL supports in (passed to stored procedures), out (outgoing from stored procedures), and inout (incoming and outgoing) parameters for stored procedures. The code for the stored procedure is in the begin and end statements, which are a series of SELECT statements that retrieve the value and then save it to the appropriate variable (via the INTO keyword)

-- 根据姓名查询学生信息,返回学生的城市delimiter ;;createprocedure select_students_by_name(    invarchar(255),    outvarchar(255-- 输出参数    int(11))    begin         selectfromwhereandinto _city;    end ;;delimiter ;

To execute a stored procedure:

set20;set'jack';call select_students_by_name(@_name, @_city, @_age);selectasas age;

Stored procedures with wildcard characters
delimiter ;;createprocedure select_students_by_likename(    invarchar(255))    begin        selectfromwherelike _likename;    end ;;delimiter ;

To execute a stored procedure:

call select_students_by_likename('%s%');call select_students_by_likename('%j%');

Adding, modifying, deleting with stored procedures increase
delimiter ;;createprocedure insert_student(    int,    varchar(255),    int,    varchar(255))    begin        insertinto students(idvalues(_id,_name,_age,_city);    end ;;delimiter ;

To execute a stored procedure:

call insert_student(5'张三'19'上海');

After execution, there is one more piece of data in the table, such as:

Modify
delimiter ;;createprocedure update_student(    int,    varchar(255),    int,    varchar(255))    begin        updatesetwhereid = _id;    end ;;delimiter ;

To execute a stored procedure:

call update_student(5'amy'22'杭州');

Delete
delimiter ;;createprocedure delete_student_by_id(    int)    begin        deletefromwhere id=_id;    end ;;delimiter ;

To execute a stored procedure:

call delete_student_by_id(5);

The record with ID 5 in the students table was successfully deleted. Such as:

Querying stored Procedures

To query all stored procedures:

selectfromwhere db='数据库名';

To query a stored procedure:

createprocedure 存储过程名;

Permanent update of this article address: https://github.com/nnngu/LearningNotes/blob/master/MySQL/01%20MySQL%20%E5%AD%98%E5%82%A8%E8%BF%87%E7% A8%8b%e7%9a%84%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8.md

Simple use of MySQL stored procedures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.