DELIMITER $$
USE `cclcloud`$$
drop PROCEDURE if exists `PROC_FLOW_SELECT`$$
create DEFINER=`root`@`localhost` PROCEDURE `PROC_FLOW_SELECT`(flowno varchar(32),poscode varchar(32), sdate_begin VARCHAR(20),sdate_end varchar(20))
BEGIN
declare tmpKCTable varchar(8000) DEFAULT '';
declare tb varchar(500);
DECLARE tb_count int DEFAULT 0;
DECLARE tb_index INT DEFAULT 0;
DECLARE table_name_cursor cursor
for
select table_name from information_schema.tables where table_schema='cclcloud' and table_name like 't_d_tradeflow%' order by table_name;
select COUNT(TABLE_NAME) INTO tb_count from information_schema.tables where table_schema='cclcloud' and table_name like 't_d_tradeflow%';
open table_name_cursor;
REPEAT
fetch next from table_name_cursor into tb;
if tb_index = 0 then
set tmpKCTable=CONCAT(tmpKCTable,' select id,flowno,poscaseid,poscode,sdate,operatorid,tradetype,qty,amount,disc_amount,pay_amount,change_amount,adjust_amount,custcode,custtype,right_nbr,scores,last_scores,securityno,settle_flag from ',tb,' where 1 = 1');
else
set tmpKCTable=CONCAT(tmpKCTable,' union all select id,flowno,poscaseid,poscode,sdate,operatorid,tradetype,qty,amount,disc_amount,pay_amount,change_amount,adjust_amount,custcode,custtype,right_nbr,scores,last_scores,securityno,settle_flag from ',tb,' where 1 = 1');
end if;
#flowno
if flowno <> '' && flowno is not null THEN
set tmpKCTable = CONCAT(tmpKCTable,' and flowno = "',flowno,'"');
end if;
#poscode
if poscode <> '' && poscode is not null THEN
set tmpKCTable = CONCAT(tmpKCTable,' and poscode = "',poscode,'"');
end if;
#sdate
if sdate_begin <> '' && sdate_begin is not null THEN
set tmpKCTable = CONCAT(tmpKCTable,' and sdate >= STR_TO_DATE(" ',sdate_begin,'" ,"%Y-%m-%d")');
end if;
if sdate_end <> '' && sdate_end is not null THEN
set tmpKCTable = CONCAT(tmpKCTable,' and sdate <= STR_TO_DATE(" ',sdate_end,'","%Y-%m-%d")');
end if;
set tb_index = tb_index + 1;
UNTIL tb_index >= tb_count
END REPEAT;
CLOSE table_name_cursor;
set @sql_select = tmpKCTable;
prepare stmt from @sql_select;
execute stmt;
DEALLOCATE prepare stmt;
END$$
DELIMITER ;
mysql 存储过程
最新推荐文章于 2021-04-14 09:41:50 发布
本文介绍了一种使用MySQL存储过程实现跨多个类似表结构的数据查询方法。通过动态SQL语句构造,该过程能够根据输入参数(如流水号、POS代码及日期范围)灵活地从一系列交易流水表中检索相关信息。
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印


5万+

被折叠的 条评论
为什么被折叠?



