Mysql- Stored Procedures

Stored Procedures

  Meaning: a collection of pre-compiled SQL statement, understood as a batch statement

  benefit:

    1, to improve the code reusability

    2, to simplify the operation

    3, reducing the number of the compiler and to reduce the number of database connections and to improve the efficiency

 

First, create the syntax:

CREATE PROCEDURE stored procedure name (parameter list)

  BEGIN

    Body of the procedure (a combination method of a SQL statement)

  END

  note:

 1, consists of three parts parameter list

    Parameter name Parameter type Parameter mode

    For example:

      IN session name VARCHAR (20)

    Parameter mode:

      IN: This parameter can be used as an input, that is, the caller needs to pass this parameter value

      OUT: The nibble as an output, i.e. the parameter as a return value

      INOUT: This parameter either as an input and as an output, which is the need to pass both the parameter values ​​and can return a value.

  2, if only one statement in the stored procedure can be omitted BEGIN END

  The end of the required weight of the stored procedure for each SQL statement must be a semicolon

  The end of the stored procedure can be used to reset DELIMITER

  grammar:

    DELIMITER end tag

    Case:

    DELIMITER $

  

Second, the call syntax:

  CALL stored procedure name (parameter list);

    1, an empty parameter list

    Case: five records inserted into the table admin

    DELIMITER $

    CREATE  PROCEDURE MYP1()

    BEGIN

       INSERT INTO admin(username,'password')

       VALUE ('join','000'),('join','111'),('join','222'),('join','333'),('join','444');

    END $

    transfer

    CALL myp1() $

 

    2, create a stored procedure in mode

    Case: create a stored procedure to achieve information based on the name of the goddess, god corresponding query

 

    CREATE PROCEDURE myp2(IN beauty VARCHAR(20) )

    BEGIN 

      SELECT bo.*

       FROM boys bo

       RIGHT JOIN beauty b ON bo.id = b.boyfriend_id

       WHERE b.name = beautyName;

    END $

 

    transfer

    CALL my2p ( 'Hsiao Ming') $ 

    Case 2: create stored procedures, whether the user login is successful

    CREATE PROCEDURE myp3(IN username VARCHAR(20), IN password VARCHAR(20))

    BEGIN 

      DECLARE result INT DEFAULT 0; # declare and initialize variables

      SELECT COUNT (*) INTO result # assignment

      FROM admin  

      WHERE admin.username = username

      AND admin.password = password;

      SELECTI IF (result> 0, 'success', 'failure'); use #

    END $

    transfer

    CALL myp3 ( 'Zhang', '8888') $

 

    Create a stored procedure with out mode

    Case 1: According to the goddess name returns the corresponding male name of God

    CREATE PROCEDURE myp4(IN beautyName VARCHAR(20),  OUT boyName VARCHAR(20))

      BEGIN 

        SELECT bo.boyName INTO boyname # assignment 

        FROM   boys   bo

        INNER JOIN beauty b  ON  bo.id = b.biyfriend_id

        WHERE b.name = beautyName;

      END $

      transfer

          #SET @ BName $ define

      CALL myp4 ( 'Zhao', @bName) $ # @ bname can define also can be used directly in the use

      SELECT @bName$

      

      Case 2: According to the goddess name, returns the corresponding male name of God and Charisma

      CREATE PROCEDURE my5( IN beautyName VARCHAR(20) ,OUT  boyName VARCHAR(20),  OUT userCP INT )

  BEGIN 

        SELECT bo.boyName, bo.userCP INTO boyName, UserCP

        FROM   boys   bo

        INNER JOIN beauty b  ON  bo.id = b.biyfriend_id

        WHERE b.name = beautyName;

      END $

      transfer

      CALL my5 ( 'Zhao', @bName, @userCP) $

      SELECT @BName , @userCP$

 

Guess you like

Origin www.cnblogs.com/GOOGnine/p/12271404.html