About MySQL stored procedures

Source: Internet
Author: User
Tags name database

A stored procedure, which can be understood as a logical SQL statement.

Advantage: Execution is very efficient, and stored procedures are performed on the server side of the database.

Disadvantage: The portability is poor, and the stored procedures of different databases are not portable.

Two

1. Basic grammar

--Create a stored procedure start--declares that the stored procedure terminator is $DELIMITER $CREATE PROCEDUREStorage Name (inchIint, Out Snamevarchar( -))--parameter type (in,out,inout) parameter name database typeBEGIN    --can write SQL and logical body    --The if statement begins with the notation    --columns such as:    ifConditional expressions Then         --can write SQL and logical bodyElseIf conditional expression Then         --can write SQL and logical body    Else         --can write SQL and logical bodyEnd If--end of the IF statement        --While Loop statement starts    --columns such as:    --declaring variable I,result    DECLAREIINT DEFAULT 1; DECLAREResultINT DEFAULT 0;  whileI<=Num do--can write SQL and logical body        SETResult=Result+i; SETI=I+1; END  while; --While Loop statement ends        --use into to get the data for the database to start with the output parameters    --columns such as:        SELECTNAME intoSname fromTable nameWHEREId=I--i is input parameter, sname is output parameter    --use into to get the data from the database to the end of the output parameterEND $
--Create a stored procedure end
--Note parameters: inch :   represents an input parameter that can carry data in a stored procedure out: Represents an output parameter that can be returned from a stored procedure inout: Represents the input and output parameters, which can either be input or output functions

2. Call the stored procedure

Call  stored procedure name (Parameters ...);

3. Delete stored Procedures

DROP PROCEDURE stored procedure name;

Third, Trigger

1. Create a grammar

CREATE  TRIGGER  Trigger Name   
After| Before
INSERT | DELETE | UPDATE on
for Each ROW
--Statement SQL
End

2. Delete

DROP  TRIGGER  Trigger name;

Yexiangyang

[Email protected]

About 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.