簡體   English   中英

如何在存儲過程中的Mysql中創建游標

[英]How to create a cursor in Mysql Within Stored procedure

我創建了一個存儲過程,我需要從投訴表中獲取所有記錄,然后遍歷它並找出誰在特定投訴時評論了什么。 您可以將其視為博客應用程序,其中博客可以有多個評論。 我收到了語法錯誤

Declare cur CURSOR statement,

可能是什么問題或我錯過了什么。

這是我得到的以下信息

錯誤代碼:1064 您的SQL語法有錯誤; 查看與您的MySQL服務器版本對應的手冊,以便在'declare cur CURSOR for SELECT id,user_id FROM complaint3 WHERE user_id IN(第22行的SELEC')附近使用正確的語法

以下是我的程序

DELIMITER $$

CREATE PROCEDURE `myDB`.`ShowResult`(user_id INT, hours INT,L_Complaint_id INT)
BEGIN
 DECLARE complaint_count INT;
 DECLARE 24Hrs_count INT;
 DECLARE 48Hrs_count INT;
 DECLARE Gr_48Hrs_count INT;
 DECLARE notCommented INT;
 DECLARE agentName VARCHAR(100);
 DECLARE v_agentId INT;
 DECLARE v_userId INT;
 DECLARE lastUserCommentDate DATETIME;
 DECLARE lastInternalUserCommentDate DATETIME;
 DECLARE tempDate INT;
 DECLARE v_complaint_id INT;

 SET 24Hrs_count =0;
 SET 48Hrs_count =0;
 SET Gr_48Hrs_count =0;
 SET notCommented =0;
 SET v_complaint_id =0;

 DECLARE cur CURSOR FOR SELECT id,user_id FROM complaint3 WHERE user_id IN( SELECT id FROM user3 WHERE user_type=0);   


CREATE TEMPORARY TABLE IF NOT EXISTS resultTable (
id MEDIUMINT NOT NULL AUTO_INCREMENT,t_agentId INT,t_agentName  VARCHAR(1000),t_24HrsCount INT,t_48HrsCount INT,t_Gr48HrsCount INT,
t_nullCount INT,PRIMARY KEY (id)) ENGINE = MEMORY;

SELECT COUNT(DISTINCT(id)) INTO complaint_count FROM complaint3 WHERE user_id IN(SELECT id FROM user3 WHERE user_type=0);
OPEN cur;
insert_loop: LOOP

 IF complaint_count > 0 THEN    
   FETCH cur INTO complaint_id,user_id;

      SELECT created_at INTO lastUserCommentDate FROM complaint3_diary WHERE complaint_id=v_complaint_id AND user_id = v_user_id ORDER BY id DESC LIMIT 1;
      SELECT assigned_to INTO v_agentId FROM assignment3 WHERE complaint_id=v_complaint_id AND a.expire_at IS NULL;
      SELECT NAME INTO agentName FROM user3 WHERE id=v_agentId;
      SELECT created_at INTO lastInternalUserCommentDate FROM complaint3_diary WHERE complaint_id=v_complaint_id AND user_id = v_agentId ORDER BY id DESC LIMIT 1;
  SELECT TIMESTAMPDIFF(HOUR, lastInternalUserCommentDate, lastUserCommentDate)  INTO tempDate;

  IF (tempDate >0 && tempDate <= 24) THEN
    SET 24Hrs_count =1;
  ELSEIF (tempDate >24 && tempDate <= 48) THEN
     SET 48Hrs_count = 1;
  ELSEIF (tempDate >48) THEN
     SET Gr_48Hrs_count = 1;
  ELSE
     SET notCommneted = 1;
   END IF;
      INSERT INTO resultTable(t_agentId,t_agentName,t_24HrsCount,t_48HrsCount,t_Gr48HrsCount,t_nullCount) VALUES(v_agentId,agentName,24Hrs_count,48Hrs_count,Gr_48Hrs_count,notCommneted);

ELSE
LEAVE insert_loop;
END IF;
 SET complaint_count = complaint_count - 1;
END LOOP;
CLOSE cur;
    SELECT t_agentId,t_agentName,COUNT(t_24HrsCount),COUNT(t_48HrsCount),COUNT(t_Gr48HrsCount),COUNT(t_nullCount) FROM resultTable GROUP BY agentId;

END$$

DELIMITER;

根據DECLARE文檔

DECLARE只允許在BEGIN ... END復合語句中使用,並且必須在其開始之前,在任何其他語句之前。

在您的代碼中,聲明

DECLARE cur CURSOR FOR 
    SELECT id, user_id 
      FROM complaint3 
     WHERE user_id IN( SELECT id FROM user3 WHERE user_type = 0 );   

沒有遵循聲明順序規則。 因此是錯誤。

更改部分代碼如下:

BEGIN
 DECLARE complaint_count INT;
 DECLARE 24Hrs_count INT;
 DECLARE 48Hrs_count INT;
 DECLARE Gr_48Hrs_count INT;
 DECLARE notCommented INT;
 DECLARE agentName VARCHAR(100);
 DECLARE v_agentId INT;
 DECLARE v_userId INT;
 DECLARE lastUserCommentDate DATETIME;
 DECLARE lastInternalUserCommentDate DATETIME;
 DECLARE tempDate INT;
 DECLARE v_complaint_id INT;

 DECLARE cur CURSOR FOR 
     SELECT id, user_id 
       FROM complaint3 
      WHERE user_id IN( SELECT id FROM user3 WHERE user_type = 0 );   

 SET 24Hrs_count =0;
 SET 48Hrs_count =0;
 SET Gr_48Hrs_count =0;
 SET notCommented =0;
 SET v_complaint_id =0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM