Stored procedures with parameters for MySql dynamic sql statements
This article mainly introduces "stored procedures with parameters in MySql dynamic sql statements". In daily operation, I believe many people have doubts about stored procedures with parameters in MySql dynamic sql statements. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "stored procedures with parameters in MySql dynamic sql statements". Next, please follow the editor to study!
The MySql stored procedure queries the user's information according to the conditions entered by the user and the sort method, and the sort condition call method:
Call GetUsersDynamic (age0 then begin set @ sqlstr=concat (select id,name,password,age,getdate (adddate) as AddDate from users where, WhereCondition, order by, OrderByExpress); end; else begin set @ sqlstr=concat (select id,name,password,age,getdate (adddate) as AddDate from users where, WhereCondition); end; end if; prepare stmt from @ sqlstr; execute stmt; end
Getdate () is a custom function that returns the short format of the date
CREATE DEFINER= `root` @ `localhost` FUNCTION `getdate` ($date datetime) RETURNS varchar (50) CHARSET latin1 return date_format ($date,%Y-%m-%d)
A MySql stored procedure that dynamically inserts data (note that four single quotation marks represent a single quote):
CREATE DEFINER= `root` @ `localhost` PROCEDURE `InsertUser` (in name2 varchar (50), in password2 varchar (32), in age2 int,in adddate2 datetime) begin DECLARE stmt varchar (2000); set @ sqlstr=concat (insert into users (name,password,age,adddate) values (); set @ sqlstr=concat (@ sqlstr,name2,password2,age2,adddate2,)); prepare stmt from @ sqlstr; execute stmt; end At this point, the study of "stored procedures with parameters in MySql dynamic sql statements" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!