2016-10-09 66 views
1

我有一個存儲過程,如下所示。Mysql存儲過程獲取數據並返回爲行

在這個過程中,查詢應該返回一個基於名稱匹配的行;但是當我嘗試使用此SR時,查詢不會提取,並且SR未成功執行。請讓我知道我的錯誤?

DROP TABLE TEMP; 

CREATE TABLE TEMP 
(
NAME VARCHAR(15) 
); 

INSERT INTO TEMP (SELECT DISTINCT(NAME) FROM IMDETAILS); 

DELIMITER $$ 
CREATE PROCEDURE INCIDENT() 
BEGIN 

DECLARE NAMEE VARCHAR(10); 

DECLARE CUR1 CURSOR for SELECT NAME FROM TEMP; 

open CUR1; 

read_loop: LOOP 

FETCH CUR1 INTO NAMEE; 

SELECT NAMEE; 

insert into incident values ((select distinct(NAME) from IMDETAILS where NAME=NAMEE), 

(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_DATE=(SELECT CURDATE()) AND STATUS='RESOLVED'), 

(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_WEEK = WEEK(CURDATE()) AND STATUS='RESOLVED'), 

(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_MONTH=(SELECT DATE_FORMAT(NOW(),'%M')) AND STATUS='RESOLVED'), 

(SELECT POINTS FROM POINT WHERE IMCOUNT<=(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_MONTH=(SELECT DATE_FORMAT(NOW(),'%M')) 
AND STATUS='RESOLVED'))); 

END LOOP; 
CLOSE CUR1; 

END $$; 

DELIMITER; 
+0

最後一個分隔符的說法應該是分隔符; –

+0

您可以添加示例數據和調用語句嗎? –

+0

我有IMDETAILS表的列名爲「名稱」,「每日計數」,「週末計數」,「每月計數」我臨時表的列爲「名稱」.so在我的商店程序中,臨時表中的名稱應該根據每天,每週,每月計數與IMDETAILS表中的名稱匹配。 – RMP

回答

0

我假設你的問題是從遊標返回0行。嘗試修改代碼以包含完成聲明,繼續處理程序聲明和完成測試。例如

DROP PROCEDURE IF EXISTS INCIDENT; 
DELIMITER $$ 

CREATE DEFINER=`root`@`localhost` PROCEDURE `INCIDENT`() 
    LANGUAGE SQL 
    NOT DETERMINISTIC 
    CONTAINS SQL 
    SQL SECURITY DEFINER 
    COMMENT '' 
BEGIN 
DECLARE done INT DEFAULT FALSE; 
DECLARE NAMEE VARCHAR(10); 
DECLARE CUR1 CURSOR for SELECT distinct NAME FROM TEMP; 

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 

DROP TABLE IF EXISTS INCIDENTS; 
CREATE TABLE INCIDENTS(NAME VARCHAR(3),RESOLVED_DATE_COUNT INT); 

open CUR1; 
read_loop: LOOP 
     FETCH CUR1 INTO NAMEE; 

     if done then leave read_loop; end if; 

     #SELECT NAMEE; 
     insert into incidents values 
     (
     NAMEE, 

     (SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_DATE= CURDATE() AND STATUS='RESOLVED') 

     #(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_WEEK = WEEK(CURDATE()) AND STATUS='RESOLVED'), 
     #(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_MONTH=(SELECT DATE_FORMAT(NOW(),'%M')) AND STATUS='RESOLVED'), 
     #(SELECT POINTS FROM POINT WHERE IMCOUNT<=(SELECT COUNT(*) FROM IMDETAILS WHERE NAME=NAMEE AND RESOLVED_MONTH=(SELECT DATE_FORMAT(NOW(),'%M')) 
     #AND STATUS='RESOLVED')) 
     ); 

END LOOP; 
CLOSE CUR1; 

END $$ 
DELIMITER ; 

注意我也改變了遊標select語句的選擇不同,插入語句選擇namee(似乎沒有在重讀imdetails當你已經知道了這麼多點),修訂了第一選擇刪除不必要的curdate選擇。 所以給

DROP TABLE IF EXISTS IMDETAILS; 

CREATE TABLE IMDETAILS(NAME VARCHAR(3),RESOLVED_DATE DATE, STATUS VARCHAR(10)); 
INSERT INTO IMDETAILS VALUES 
('ABC','2016-10-10','RESOLVED'),('ABC','2016-10-10',NULL),('ABC','2016-10-10','RESOLVED'),('ABC','2016-10-10','RESOLVED'), 
('DEF','2016-10-10',NULL),('DEF','2016-10-10',NULL),('DEF','2016-10-10',NULL),('DEF','2016-10-10','RESOLVED'); 

DROP TABLE IF EXISTS TEMP; 
CREATE TABLE IF NOT EXISTS TEMP AS 
SELECT NAME FROM IMDETAILS WHERE NAME = 'ABC'; 

CALL INCIDENT(); 

SELECT * FROM INCIDENTS; 

結果

+------+---------------------+ 
| NAME | RESOLVED_DATE_COUNT | 
+------+---------------------+ 
| ABC |     3 | 
+------+---------------------+