动态SQL语句
BEGIN
DECLARE g_id, g_sum INT DEFAULT 0;
DECLARE mark INT DEFAULT 0; #定义状态变量(写在上边) #所有的DECLARE 必须写在上面
DECLARE curl CURSOR FOR SELECT goods_id FROM goods_view; #创建游标 视图名
DECLARE CONTINUE HANDLER FOR NOT found SET mark=1; #处理性事件
#------------------ 必须用**@allsql 会话变量**
SET @allsql=`自定义函数`("CREATE VIEW goods_view AS"," SELECT * FROM goods ",sqlwhere); # 创建视图字符串 sqlwhere是方法参数
DROP VIEW IF EXISTS goods_view;
PREPARE stmt FROM @allsql;
EXECUTE stmt; # 执行视图
DEALLOCATE PREPARE stmt;
#------------------
OPEN curl; #打开游标, 数据查询
REPEAT
SET g_id=0;
FETCH curl INTO g_id; #读取每行记录, 和定义对应 定义有几列, 就定义几个变量
IF g_id>0 THEN
SET g_sum=g_sum+g_id;
END IF;
UNTIL mark=1 #如果没行则退出循环
END REPEAT;
CLOSE curl; # 关闭游标
SELECT g_sum;
END
附表(在此末尾)
https://blog.csdn.net/qq_21480147/article/details/98944652