程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 數據-sql語句出錯,求大神幫忙解答

數據-sql語句出錯,求大神幫忙解答

編輯:編程綜合問答
sql語句出錯,求大神幫忙解答

CREATE PROCEDURE test()
BEGIN

DECLARE fid int;

DECLARE days int DEFAULT 1;

DECLARE s int default 0;

DECLARE company_code VARCHAR;

DECLARE cursor_company CURSOR FOR SELECT company FROM test WHERE status = 3;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET a=1;

OPEN cursor_company;

FETCH cursor_company INTO company_code;
WHILE a<>1 DO

        IF company_code = 'DD' THEN
  SET days = 2;
  END IF;
  ELSEIF company_code ='ZD' THEN 
  SET days = 5;
  END ELSEIF;
  ELSEIF company_code ='GG' THEN
  SET days = '8';
  END ELSEIF;
  FETCH  cursor_company INTO company_code;

                DECLARE cursor_name CURSOR FOR SELECT id FROM test WHERE UNIX_TIMESTAMP(check_time) < UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -days DAY));
                #設置一個終止標記
                DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;
                #打開游標
                OPEN cursor_name;
                #獲取游標當前指針的記錄,讀取一行數據並傳給變量stu
                FETCH cursor_name INTO fid;
                #開始循環,判斷是否游標已經到達了最後作為循環條件
                WHILE s <> 1 do  
                    UPDATE test SET status = 8 where id=fid; 
                 #讀取下一行的數據  
                    FETCH  cursor_name INTO fid; 
                END WHILE;  
                 #關閉游標  
                CLOSE cursor_name ; 
                #語句執行結束  

END WHILE;
CLOSE cursor_company;

END

表結構

id int

check_time datetime

status int

company varchar

錯誤信息:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';

DECLARE cursor_company CURSOR FOR SELECT company FROM test WHERE status =' at line 10

看了好久,可是感覺沒有錯誤,不知道毛病出在哪裡,求大神解答

最佳回答:


CREATE PROCEDURE test ()
BEGIN
DECLARE
fid INT;

DECLARE
days INT DEFAULT 1;

DECLARE
s INT DEFAULT 0;

DECLARE
a INT DEFAULT 0;

DECLARE
company_code VARCHAR (50);

DECLARE
cursor_company CURSOR FOR SELECT
company
FROM
test
WHERE
STATUS = 3;

DECLARE
cursor_name CURSOR FOR SELECT
id
FROM
test
WHERE
UNIX_TIMESTAMP(check_time) < UNIX_TIMESTAMP(
DATE_ADD(NOW(), INTERVAL - days DAY)
);

#設置一個終止標記
DECLARE
CONTINUE HANDLER FOR SQLSTATE '02000'
SET s = 1;

OPEN cursor_company;

FETCH cursor_company INTO company_code;

WHILE s <> 1 DO

IF company_code = 'DD' THEN

SET days = 2;

ELSEIF company_code = 'ZD' THEN

SET days = 5;

ELSEIF company_code = 'GG' THEN

SET days = '8';

END IF;

FETCH cursor_company INTO company_code;

#打開游標
OPEN cursor_name;

#獲取游標當前指針的記錄,讀取一行數據並傳給變量stu
FETCH cursor_name INTO fid;

#開始循環,判斷是否游標已經到達了最後作為循環條件
WHILE s <> 1 DO
UPDATE test
SET STATUS = 8
WHERE
id = fid;
set s = 0;
#讀取下一行的數據

FETCH cursor_name INTO fid;

END
WHILE;

#關閉游標

CLOSE cursor_name;

#語句執行結束

END
WHILE;

CLOSE cursor_company;

END

給你格式化了一下
不然有點亂亂
DECLARE
CONTINUE HANDLER FOR SQLSTATE '02000'
這個只定義一個

ayanami001
liumohan0806
ayanami001
liumohan0806
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved