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

时间:2018-06-12 05:05:29

标签: mysql loops cursor temp-tables

我有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"在semicolan之后。但这结束了Stired proc。我没有得到什么是正确的语法来实现这一点。我已经做过r& D了。但是从所有参考文献中,我得到了相同的语法。你能告诉我这里缺少什么吗?

1 个答案:

答案 0 :(得分:3)

请参阅:

  

13.6.3 DECLARE Syntax

     

...

     

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

...
相关问题