MySQL stored procedure writing-Dynamic parameter application

Source: Internet
Author: User

--Deletedrop procedure if exists up_common_select

--CreateCREATE PROCEDURE `up_common_select`
(
in t_name varchar(50)
)
begin
declare v_sql varchar(500);
set v_sql= concat('select * from ',t_name);
select v_sql;
--注意:prepare(预处理)execute stmt using @var,只能跟@var变量,declare和传入的变量不行!!!
set @v_sql=v_sql;
prepare stmt from @v_sql;
EXECUTE stmt ;
deallocate prepare stmt;
end;

--Callcall up_common_select('admin_authority');

Attention matters

1 mysql5.0.13 supports calling prepare in stored procedures

2 Prepare stmt from ' select * '; Wrong

Mysql5.0.24,prepare does not support table name to do variable!

Solution: Combine strings with the Contat () function

3 Execute stmt [using @var, @var2]

Must be a variable in the form of @var, parameter variable passed in, declare variable not

4. deallocate prepare stmt; Explicit release of Prepare, if not released, MySQL will be released!

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.