gpt4 book ai didi

Mysql - 过程中的 IN 子句用于在过程中声明游标

转载 作者:行者123 更新时间:2023-11-30 00:42:55 27 4
gpt4 key购买 nike

请帮帮我..我声明带有 IN 子句值的游标,该值从其他表获取,其中该值将用逗号 (,) 分隔,并将其存储到变量中并提供给 IN 子句。

这是我的代码:

CREATE  DEFINER=`xxx`@`%`  PROCEDURE `procedurename`(
OUT examDetails VARCHAR(2500)
)
BEGIN

DECLARE v_examType VARCHAR(50);
DECLARE v_minMarks INT(10);
DECLARE v_time INT(10);
DECLARE v_noOfQuestions INT(10);
DECLARE v_subTopicIds VARCHAR(50);
DECLARE v_subtopicname VARCHAR(50);
DECLARE v_result VARCHAR(2500) DEFAULT '';
DECLARE v_temp INT(20);



DECLARE cur1 CURSOR FOR SELECT ExamType,MinMarks,TIME,NoOfQuestions,SubTopicIds FROM tblCreSetExam;


select ExamType,MinMarks,Time,NoOfQuestions,SubTopicIds from tblCreSetExam
OPEN cur1;
FETCH FROM cur1 INTO v_examType,v_minMarks,v_time,v_noOfQuestions,v_subTopicIds;

declare cur2 cursor for select SubTopicName from tblEcertSubTopics where Id in (v_subTopicIds);




v_result = CONCAT(v_result,v_examType,'!',v_minMarks,'!',v_time,'!',v_noOfQuestions,'!');
open cur2;
fetch from cur2 into v_subtopicname;

v_result = CONCAT(v_result,v_subtopicname,'!');

close cur2;
v_temp = LENGTH(v_result)-1;
v_result = SUBSTRING(v_result,1,v_temp);
v_result = CONCAT(v_result,'^')


CLOSE cur1;



SET examDetails = v_result;

END$$

DELIMITER ;

但我收到此错误:

Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your
MySQLserver version for the right syntax to use near 'declare cur2 cursor for select
column1 from table1 where Id in (' at line 20

有人知道为什么会这样吗?提前致谢

最佳答案

您的代码中存在多个错误。

错误 1:

FETCH FROM cur1 INTO v_examType,v_minMarks,v_time,v_noOfQuestions,v_subTopicIds;

将其更改为:

FETCH cur1 INTO v_examType,v_minMarks,v_time,v_noOfQuestions,v_subTopicIds;

错误是由于 FROM 的存在造成的。这不是必需的。

错误 2:

改变

v_result = CONCAT(v_result,'^')

致:

v_result = CONCAT(v_result,'^');

缺少分号。

关于Mysql - 过程中的 IN 子句用于在过程中声明游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21624314/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com