MYSQL---stored procedures

Source: Internet
Author: User

Stored procedures with no parameters

CREATE PROCEDURE SP1 ()

SELECT VERSION ();

Calling a stored procedure: Call SP1//stored procedure without parameters can be called without parentheses

DELIMITER//

Stored procedure with in parameter

CREATE PROCEDURE Removeuserbyid (in uid INT UNSIGNED)

BEGIN

DELETE from users WHERE id=uid;

end//

Calling the stored procedure: Call Removeuserbyid (3);

To delete a stored procedure

DROP PROCEDURE [IF EXISTS] Removeuserbyid

Stored procedures with in and out parameters

DELIMITER//

CREATE PROCEDURE removeuserandreturnusernums (in uid INT unsigned,out usernums INT UNSIGNED)

BEGIN

DELETE from users WHERE id=uid;

SELECT COUNT (ID) from the users into Usernums;

end//

DELIMITER;

Call:

Call Removeuserandreturnusernum (@nums);

SELECT @nums;

Stored procedures with multiple Outo type parameters

DELIMITER//

CREATE PROCEDURE Removeuserbyageandreturninfos (in p_age int unsigned,out deleteusers int unsigned,out UserCount int UNSIG NED)

BEGIN

DELETE from users WHERE age=p_age;

SELECT Row_count () into deleteusers;

SELECT COUNT (ID) from the users into UserCount;

end//

DELIMITER;

Call Removeuserbyageandreturninfos (20,@A,@B);

SELECT @a,@b;

MYSQL---stored procedures

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.