Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在存储过程MYSQL中创建临时表_Mysql_Loops_Cursor_Temp Tables - Fatal编程技术网

在存储过程MYSQL中创建临时表

在存储过程MYSQL中创建临时表,mysql,loops,cursor,temp-tables,Mysql,Loops,Cursor,Temp Tables,我有SQL SERVER的经验。这是我第一次使用MYSQL。我想在存储过程中创建一个临时表。我不确定我在这里错过了什么。 我想做的是: 循环遍历事件,然后遍历其匹配项,并将这些匹配项插入到临时表中,然后从该临时表返回结果。 这是我的存储过程代码 CREATE DEFINER=`root`@`localhost` PROCEDURE `APP_GetMatchListbyScoreboardOperatorID`(SOID int) BEGIN DECLARE eventid INT DEFAUL

我有SQL SERVER的经验。这是我第一次使用MYSQL。我想在存储过程中创建一个临时表。我不确定我在这里错过了什么。 我想做的是: 循环遍历事件,然后遍历其匹配项,并将这些匹配项插入到临时表中,然后从该临时表返回结果。 这是我的存储过程代码

CREATE DEFINER=`root`@`localhost` PROCEDURE `APP_GetMatchListbyScoreboardOperatorID`(SOID int)
BEGIN
DECLARE eventid INT DEFAULT NULL;

DECLARE done1, done2 BOOLEAN DEFAULT FALSE;  
DECLARE eventname varchar(500);
DECLARE eventdate varchar(100);
DECLARE numberOfMats int;
DECLARE backgroundLogo varchar(1500);
DECLARE categoryid int;
DECLARE categoryname varchar(500);
DECLARE sheettitle varchar(2000);
DECLARE matchid int;
DECLARE bracketmatchid int;
DECLARE parentid int;
DECLARE competitor1 long;
DECLARE competitor2 long;
DECLARE round int;
DECLARE matcheStatusDisplay varchar(500);
DECLARE sheetid long;
DECLARE matnumber int;
DECLARE starttime float;
DECLARE duration_category long;
DECLARE categorytimelimit int;
DECLARE numberoffights_category int;


CREATE TEMPORARY TABLE TempTable (eventid int) ; 
#DECLARE done TINYINT DEFAULT FALSE;
-- declare a cursor to select the desired columns from the desired source table1
-- the input argument (which you might or might not need) is used in this example for row selection
DECLARE cursor_events -- cursor1 is an arbitrary label, an identifier for the cursor
 CURSOR FOR SELECT EventId FROM scoreboardoperatoreventmapping WHERE ScoreboardOperatorID =SOID; 
-- this fancy spacing is of course not required; all of this could go on the same line.
-- a cursor that runs out of data throws an exception; we need to catch this.
-- when the NOT FOUND condition fires, "done" -- which defaults to FALSE -- will be set to true,
-- and since this is a CONTINUE handler, execution continues with the next statement.   
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = TRUE;
-- open the cursor
OPEN cursor_events;
my_loop: -- loops have to have an arbitrary label; it's used to leave the loop
LOOP
  -- read the values from the next row that is available in the cursor
FETCH cursor_events INTO eventid;
  IF done1 THEN -- this will be true when we are out of rows to read, so we go to the statement after END LOOP.
   LEAVE my_loop; 
  ELSE -- val1 and val2 will be the next values from c1 and c2 in table t1, 
  -- so now we call the procedure with them for this "row"
  BLOCK1 : BEGIN
DECLARE cur2 CURSOR FOR 
    Select e.eventname,e.eventdate,e.numberOfMats,e.backgroundLogo, s.categoryid,s.categoryname,s.sheettitle,m.matchid,m.bracketmatchid,m.parentid,m.competitor1,m.competitor2,m.round,ms.MatchStatus as matcheStatusDisplay,
                                        s.sheetid,s.matnumber,s.starttime,s.duration_category,s.categorytimelimit,s.numberoffights_category
            from events e 
            LEFT JOIN matches m on e.eventid= m.eventid  AND m.eventid=eventId
            LEFT JOIN matchstatus ms on m.matcheStatus=ms.Id AND m.matcheStatus in (select id from matchstatus where (matcheStatus!='Completed')) 
            LEFT JOIN sheets s on s.sheetid=m.sheetid AND s.eventid=eventId
            where e.eventid=eventId and m.round!=-1 order by  matnumber, starttime , categoryid, round, parentid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;


open cur2;
loop2 : LOOP
FETCH cur2 INTO eventname,eventdate,numberOfMats,backgroundLogo, categoryid,categoryname,sheettitle,
            matchid,bracketmatchid,parentid,competitor1,competitor2,round,matcheStatusDisplay,
            sheetid,matnumber,starttime,duration_category,categorytimelimit,numberoffights_category;  
    if done2 THEN
    CLOSE cur2;
    SET done2 = FALSE;
    LEAVE loop2;
    end if;
    select eventId,matchid,eventname,4;

END LOOP loop2;
END BLOCK1;
-- maybe do more stuff here
  END IF;
END LOOP;
 select 4;
END
我在创建临时表时遇到了错误,它要求在半圆之后添加“END”。但这就结束了这个激动人心的过程。我没有得到什么是正确的语法来实现这一点。我也做过同样的研发。但是从所有的参考资料中,我得到了相同的语法。你能告诉我我遗漏了什么吗

见:

只允许在复合语句中使用,并且必须在其开头,在任何其他语句之前

尝试:

。。。
声明类别int的数量;
/*创建临时表(eventid int)*/
#声明done TINYINT默认值为FALSE;
为游标声明游标\事件游标
选择EventId
从记分板操作员或事件映射
其中ScoreboardOperatorID=SOID;
为未找到的集合done1声明CONTINUE处理程序=TRUE;
创建临时表(eventid int);
--打开光标
...
请参见:

只允许在复合语句中使用,并且必须在其开头,在任何其他语句之前

尝试:

。。。
声明类别int的数量;
/*创建临时表(eventid int)*/
#声明done TINYINT默认值为FALSE;
为游标声明游标\事件游标
选择EventId
从记分板操作员或事件映射
其中ScoreboardOperatorID=SOID;
为未找到的集合done1声明CONTINUE处理程序=TRUE;
创建临时表(eventid int);
--打开光标
...

您是否尝试过分隔符$$CREATE过程。。。。。结束$$分隔符;我在你的代码里找不到。另外,如果您可以共享错误。它将帮助我们找出问题。您是否尝试过分隔符$$CREATE PROCEDURE。。。。。结束$$分隔符;我在你的代码里找不到。另外,如果您可以共享错误。这将有助于我们找出问题所在。