Start MySQL (about Stored Procedures) 3

Source: Internet
Author: User

When it comes to databases without stored procedures, it is definitely not feasible. Below we simply write two stored procedures and then the previous database.

1 without output parameters (Add User)

Drop   Procedure   If   Exists Proc_add;
Create   Procedure Proc_add (UID Varchar ( 50 ), PWD Varchar ( 20 ), Name Varchar ( 20 ))
Begin
Insert   Into T_user (u_uid, u_pwd, u_name) Values (UID, PWD, name );
End ;
Call proc_add ( ' ABCD ' , ' 123 ' , ' User 1 ' );
Select   *   From T_user;

Notes:

@ Is not required for Variable @

The "()" after the stored procedure name is required. Even if there is no parameter, "()" is required.

Do not add the as keyword

If the Stored Procedure contains multiple MySQL statements, the begin end keyword is required.

The semicolon ";" must be added at the end of each statement in the stored procedure.

You cannot use the "return" keyword in the MySQL stored procedure.

2. Stored Procedures with output parameters (user login)

Drop   Procedure   If   Exists Proc_login;
Create   Procedure Proc_login (
UID Varchar ( 50 ),
PWD Varchar ( 20 ),
Out back Int -- 0 failed 1 succeeded
)
Begin
Select U_uid From T_user Where U_uid = UID And U_pwd = PWD;
Set   @ Rowcount = Found_rows ();
If ( @ Rowcount > 0 ) Then
Set Back = 1 ;
Else
Set Back = 0 ;
End   If ;
End ;
Call proc_login ( ' Admin ' , ' 123 ' , @ B );
Select   @ B   As BBB;

 

Return the number of rows in the Select Operation. Use the found_rows () function; Use the row_count () function for the number of rows affected by the insert, update, and delete operations.

 

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.