1.首先mysql 中的 '分隔符' 是分号(;)

所以在创建存储过程的时候,要先重新定义一下分隔符。这样,存储过程中的语句才不会被当成正常的语句执行。

DELIMITER //
DROP PROCEDURE IF EXISTS test_cursor //
create procedure getAllTypesOfCards(in cardId long, in userId long, in classId long, in cardType int, in isNew boolean)
begin
declare userIdTmp long;
declare DONE int;

declare userIdCur cursor for 
select user1_id as userid from user_relation_table where relation = 'FRIEND' and user2_id = userId 
 union select user2_id as userid from user_relation_table where relation = 'FRIEND' and user1_id = userId;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET DONE = 1;
open userIdCur;
REPEAT
fetch userIdCur into userIdTmp;
if isNew = true then
    select * from card_table where cardId < id and state = 0 and (`type` = cardType or class_id = classId or user_id = userIdTmp)
    order by id desc limit 10;
else 
    select * from card_table where cardId > id and state = 0 and (`type` = cardType or class_id = classId or user_id = userIdTmp)
    order by id desc limit 10;
end if;
UNTIL DONE 
END REPEAT;
close userIdCur;
end;//

DELIMITER ;
call getAllTypesOfCards(129427,163,1,1,false);
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-10-26
猜你喜欢
  • 2021-10-04
  • 2021-10-30
  • 2021-11-16
  • 2021-09-26
  • 2022-02-27
  • 2021-09-13
  • 2021-12-04
相关资源
相似解决方案