Example of a MySQL stored procedure with Parameters

Source: Internet
Author: User

A small example of MySQL stored procedure with parameters the Stored Procedure P_GET_CLASS_NAME is based on the input class number to determine the class name Stored Procedure P_INSERT_STUDENT is to receive input student information, and finally insert information into the student table. Www.2cto.com [SQL] DROP PROCEDURE IF EXISTS 'P _ GET_CLASS_NAME '; CREATE PROCEDURE P_GET_CLASS_NAME (IN ID int, OUT NAME VARCHAR (50) BEGIN IF (ID = 1) then set name = 'first class'; end if; IF (ID = 2) then set name = 'second class'; end if; END; drop procedure if exists 'P _ INSERT_STUDENT '; create procedure P_INSERT_STUDENT (in id int, in name varchar (10), in classno int, in birth datetime) begin set @ ID = ID; SET @ NAME = NA ME; SET @ CLASSNO = CLASSNO; SET @ BIRTH = BIRTH; SET @ CLASSNAME = NULL; CALL P_GET_CLASS_NAME (@ CLASSNO, @ CLASSNAME ); SET @ insertSql = CONCAT ('insert INTO TBL_STUDENT VALUES (?,?,?,?) '); PREPARE stmtinsert FROM @ insertSql; EXECUTE stmtinsert USING @ ID, @ NAME, @ CLASSNAME, @ BIRTH; deallocate prepare stmtinsert; END; CALL P_INSERT_STUDENT (1, 'xu Yue ', 1, '2017-10-01 10:20:01 '); in the second stored procedure, ① the parameter is declared using SET, the first stored procedure is called. ② the NAME parameter in the first stored procedure is the output parameter, therefore, the @ CLASSNAME parameter is assigned a value after the first process is called. ③ use CONCAT to concatenate an SQL statement and pass in the parameter to execute the SQL statement CALL P_INSERT_STUDENT (1, 'xu Yue ', 1, '2017-10-01 10:20:01 '); call the Stored Procedure

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.