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

wko9yo5t  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(431)

我有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”。但这结束了这一过程。我没有得到什么是正确的语法来实现这一点。我也做过同样的研发。但是从所有的参考资料来看,我得到了同样的语法。你能告诉我这里缺了什么吗。

8yparm6h

8yparm6h1#

请参见:
13.6.3声明语法
... DECLARE 只允许在 BEGIN ... END 复合语句,并且必须在其开头,在任何其他语句之前。
...
尝试:

...

DECLARE numberoffights_category int;

/* CREATE TEMPORARY TABLE TempTable (eventid int); */

# DECLARE done TINYINT DEFAULT FALSE;

DECLARE cursor_events CURSOR FOR
  SELECT EventId
  FROM scoreboardoperatoreventmapping
  WHERE ScoreboardOperatorID =SOID; 

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = TRUE;

CREATE TEMPORARY TABLE TempTable (eventid int);

-- open the cursor

...

相关问题