mysql存储过程执行拼装SQL的例子

发布时间:2020-06-16编辑:脚本学堂
mysql存储过程执行拼装SQL,示例代码:

mysql/procedure/ target=_blank class=infotextkey>mysql存储过程执行拼装SQL,示例代码:
 

复制代码 代码如下:

CREATE PROCEDURE unsky_resource_procedure(IN coopnum INT)
BEGIN
SET @coopNu = coopnum;
SET @yesterday = DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL -1 DAY),'%Y_%m_%d');
SET @ddd = CONCAT('INSERT INTO unsky_t_resource(client_type,res_id,res_name,res_catalog,res_fee,order_fee,order_uv,order_volumes,order_avg_fee,order_avg_vol,created_date)
SELECT 'A',d.book_id,r.name AS rn, r.content_class, d.fee_id, SUM(d.fee_id), COUNT(DISTINCT d.mobile_number) AS uv, COUNT(d.cmread_chapter_id) AS vols,FORMAT(SUM(d.fee_id)/COUNT(DISTINCT d.mobile_number),2),FORMAT(COUNT(d.cmread_chapter_id)/COUNT(DISTINCT d.mobile_number),2),NOW()FROM day_ordered_',@yesterday,'  d, resource r WHERE d.cmread_content_id = r.cmread_content_id AND d.fee_req_msg = 0 AND d.coopnum = ? GROUP BY d.cmread_content_id');
PREPARE ttt FROM @ddd;
EXECUTE ttt USING @coopNu;

END

注解
执行拼装SQL的时候需要Prepare 和Execute这两个命令,平常的DML是不行的。