函数:
##获得当前案卷目录节点下所有的子节点
##参数—— nodeid 当前案卷目录节点
## corpid 全宗id
## 返回结果:例如 -99,351144,351149,351150,351151,351152,353138,353139
create function queryChildrenAreaInfo(nodeid int,corpid_ int)
return varchar(4000)
begin
DECLARE sTemp VARCHAR (4000);
DECLARE sTempChd VARCHAR (4000);
set sTemp = '-99';
set sTempChd = cast(nodeid AS CHAR);
while sTempChd is not null do
set sTemp = CONCAT(sTemp, ',', sTempChd);
select group_concat(id) as bb into sTempChd from menu where (corpid = corpid_ or corpid = 0) and FIND_IN_SET(pid, sTempChd) > 0;
end while;
return sTemp;
end;
存储过程:
##统计出当前菜单节点下所有节点对应的表中的数据的一个总和
#参数—— nodeId int 当前节点id(菜单id)
# corpid int 全宗id
create procedure getCurrentNodeIsDataCount(nodeId int,corpid int)
begin
DECLARE tbname_ varchar(100); ##用于接收每条记录中的表名
DECLARE nodes varchar(4000); ##用于接收当前菜单节点下所有节点的一个字符串
DECLARE tempStr varchar(50); ##用于接收每一个子节点
DECLARE sql_ varchar(3000); ##用于接收动态sql语句
DECLARE count int; ##用于做统计总记录数
##调用函数获得所有子节点的一个字符串 例如:-99,351144,351149,351150,351151,351152,353138,353139
select queryChildrenAreaInfo(nodeId,corpid) into nodes;
set nodes = CONCAT(nodes,','); #在所有节点的字符串最后加上一个 ',' 符号,方便后面字符串截取
set count=0;
##循环
loop_label:LOOP
##截取出所有子节点中的第一个节点
select SUBSTRING(nodes,1,LOCATE(',',nodes)-1) into tempStr;
##将除去第一个节点后的字符串重新赋值给nodes
select SUBSTRING(nodes,LOCATE(',',nodes)+1) into nodes;
##根据id查询,并获得表名
select tbname into tbname_ from menu where id =tempStr and tbname !='' and tbname is not null;
#判断查询到的表名是否为空
if (tbname_ is not null) THEN
##统计该表下的总记录数sql语句
set sql_ = concat('select count(*) into @tempB from ',tbname_);
set @sql_2 = sql_;
#预处理动态sql语句
prepare stmt_text from @sql_2;
#执行动态sql
execute stmt_text;
#关闭预处理
deallocate prepare stmt_text;
## 获得预处理执行语句中into的值,并进行总记录数叠加
set count = count+@tempB;
end if;
##当nodes为空时,退出循环
if (nodes = '') THEN
leave loop_label;
end if;
END LOOP;
#输出结果集
select count;
end