mysql創建存儲過程 mysql 存儲過程有參數輸入拼接 mysql存儲過程含輸入參數的拼接的分頁 ...
mysql創建存儲過程
DROP PROCEDURE IF EXISTS getCreateTimes /*前面要寫DELIMITER $$ 或DELIMITER // */ DELIMITER $$ CREATE PROCEDURE `getCreateTimes`() BEGIN SELECT userCreateTime FROM users; END;
mysql 存儲過程有參數輸入拼接
DROP PROCEDURE IF EXISTS getTest01; DELIMITER $$ CREATE PROCEDURE `getTest01`( uname VARCHAR(50), upass VARCHAR(50) ) BEGIN SET @sql= 'SELECT * FROM users where 1=1'; IF uname IS NOT NULL THEN SET @sql=CONCAT(@sql,' AND userName=',"'",uname,"'"); END IF; IF upass IS NOT NULL THEN SET @sql=CONCAT(@sql,' AND password=',"'",upass,"'"); END IF; PREPARE stmt FROM @sql; EXECUTE stmt; END; CALL getTest01(NULL,"123");
mysql存儲過程含輸入參數的拼接的分頁
DROP PROCEDURE IF EXISTS getTest01; DELIMITER $$ CREATE PROCEDURE `getTest01`( startPage INT,/*第startPage頁,從0開始算*/ pageSize INT,/*每頁顯示的記錄數*/ uname VARCHAR(50), upass VARCHAR(50) ) BEGIN SET @sql= 'SELECT * FROM users where 1=1'; IF uname IS NOT NULL THEN SET @sql=CONCAT(@sql,' AND userName=',"'",uname,"'"); END IF; IF upass IS NOT NULL THEN SET @sql=CONCAT(@sql,' AND password=',"'",upass,"'"); END IF; SET @sql=CONCAT(@sql,' LIMIT ',startPage*pageSize,",",pageSize); PREPARE stmt FROM @sql; EXECUTE stmt; END; CALL getTest01(4,2,NULL,NULL);