A little example of the MySQL database stored procedure

Source: Internet
Author: User

MySQL stored procedures:encapsulating SQL :
        
CREATE PROCEDURE P1 () beginselect  * from T_news;end $         //mysql stored procedure Simple instance
Display Stored procedure information: \g (horizontal table with vertical table output)



Delimiter $ change execution symbol until MySQL encounters $ start Execution statement command set names fix mysql garbled problem but MySQL restarts and reverts to the previous character set state
Call stored procedure name () calls stored procedure
Parameters:   
CREATE PROCEDURE p2 (n int)    #含参beginselect  * from t_category where CID > N;end $      


Control structure:       
CREATE PROCEDURE p3 (n int, J char (1))    #含参begin  if j= ' h '  then       #与其他语言不同 must be added then note symbol = not = = Select  * fro M t_category where cid > N;elseselect  * from t_category where CID <n;   End If;end $    


Computes the and of 1....N: &NBSP;&NBSP;
CREATE PROCEDURE P4 (n smallint)    #含参begin  Declare  i int;declare s int;set s = 0;set i = 1;while i<=n doset s =s+i;set i=i+1;end while;select S;end $      


The difference between stored procedures and functions:           Name differs: Stored procedure: Procedure function stored procedure does not return a value

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.