Example of creating a MySQL Stored Procedure

Source: Internet
Author: User

The following describes how to create a MySQL stored procedure and describes the detailed steps. If you have encountered any problems with the MySQL stored procedure, you may wish to take a look at it.

-- Select database

 
 
  1. mysql> use test;  
  2. Database changed 

-- Create an example table

 
 
  1. mysql> create table zzm(  
  2. -> id int primary key auto_increment,  
  3. -> name varchar(10)  
  4. -> );  
  5. Query OK, 0 rows affected (0.20 sec)  
  6. mysql> insert into zzm(name) values('zhang');  
  7. Query OK, 1 row affected (0.08 sec)  
  8. mysql> insert into zzm(name) values('zeng');  
  9. Query OK, 1 row affected (0.05 sec)  
  10. mysql> insert into zzm(name) values('ming');  
  11. Query OK, 1 row affected (0.05 sec)  
  12. mysql> select * from zzm;  
  13. +----+-------+  
  14. | id | name |  
  15. +----+-------+  
  16. | 1 | zhang |  
  17. | 2 | zeng |  
  18. | 3 | ming |  
  19. +----+-------+  
  20. 3 rows in set (0.00 sec)  

-- Change the command Terminator because the default command Terminator -- semicolon (;) is often used in procedure (;)
-- Therefore, when creating a procedure, you need to define a new terminator to indicate that the command for creating a procedure ends)
-- Change the end symbol to the dollar sign -- $
Mysql> delimiter $
-- Create MySQL Stored Procedure p3
-- The stored procedure is named p3, which contains two parameters,
-- One is IN for the input type). The parameter name is nameid and the type is int,
-- One is the output type marked with OUT), the parameter name is person_name, And the type is varchar (10)
-- This stored procedure is used to query all the contents of the zzm table and output the result set data set). Then
-- Query the field name whose ID is recorded in the table and output it to the second output type parameter.
-- No result set is output.

 
 
  1. mysql> create procedure p3(IN nameid int, OUT person_name varchar(10))  
  2. -> begin  
  3. -> select * from test.zzm;  
  4. -> select zzm.name into person_name from test.zzm where zzm.id = nameid;  
  5. -> end  
  6. -> $  
  7. Query OK, 0 rows affected (0.00 sec)  

Mysql Stored Procedure Call Method

Use of mysql ifnull Functions

MySQL date functions

Provides you with an in-depth understanding of the MYSQL Cast function.

Learn more about mysql CONCAT () Functions

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.