2013-12-17 169 views
-1

我無法弄清下列查詢語法錯誤。幫助?幫幫我 ?幫幫我 ?語法錯誤在MYSQL'1064'

CREATE PROCEDURE VoucherNumberGenerationForClass(
p_voucherType varchar(50), 
p_yearId varchar(50), 
p_NO longtext) 
begin 
IF((SELECT COUNT(suffixPrefixId)FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType))<>0) 
    then 
     DECLARE p_prefix VARCHAR(50); 
     DECLARE p_startIndex int ; 
     DECLARE p_suffix VARCHAR(50) ; 
     DECLARE p_voucherNo VARCHAR(50) ; 
     DECLARE p_prefix1 VARCHAR(50) ; 
     DECLARE p_suffix1 VARCHAR(50) ; 
      IF (((SELECT COUNT(suffixPrefixId)FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType)))<>0); 
        then 
         SET p_prefix=(SELECT ifnull(prefix,'') FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType)); 
         SET p_suffix=(SELECT ifnull(suffix,'') FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType)); 

         SET p_startIndex=(SELECT startIndex FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType)); 
         SET p_startIndex=p_startIndex-1; 

          IF p_voucherType='Sales Invoice' 
           then SET p_voucherNo=(SELECT ifnull(MAX(voucherNo+1),1) FROM tbl_SalesMaster WHERE((suffixPrefixId = (SELECT suffixPrefixId FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType))) 
           )) 
          END if ; 
         SET p_voucherNo=p_voucherNo+p_startIndex ; 
         SET p_voucherNo= p_prefix+ p_voucherNo+p_suffix ; 
         SELECT p_voucherNo ; 
      END if; 
ELSEIF 
    IF p_voucherType='Sales Invoice' 
     then SET p_voucherNo=(SELECT ifnull(MAX(voucherNo+1), 1) FROM tbl_SalesMaster) ; 
    SELECT p_voucherNo ; 
END IF ; 
end ; 
 
ERROR 1064 (42000): 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 'DECLA 
RE p_prefix VARCHAR(50); 
DECLARE p_startIndex int ; 
DECLARE p_suffix VARCHA' at line 8
+0

我想你需要'ELSEIF'中的空間。像這樣'ELSE IF' – Nithesh

+0

ELSE IF在mssql而不是mysql中。 mysql has ELSEIF – Nisar

+0

爲什麼geven我-2;( – Nisar

回答

1

獨立COUNT(suffixPrefixId)FROMCOUNT(suffixPrefixId)FROM

+0

謝謝,但我再次在第8行有一個錯誤 – Nisar

1

嘗試從以下if語句刪除分號:

IF (((SELECT COUNT(suffixPrefixId)FROM tbl_SuffixPrefix WHERE (yearId = p_yearId) AND (voucherType = p_voucherType)))<>0); 
+0

謝謝,但我再次在第8行有一個錯誤 – Nisar

3

嘗試把你的DECLARE statements權之後BEGIN

DECLARE僅在BEGIN ... END複合語句中被允許,並且必須在任何其他語句之前開始。

+0

謝謝,但我又有一個錯誤26行 - – Nisar

+0

...所以你在別的地方有另一個語法錯誤 – Gumbo

+0

@Gumbo其實這是OPs基本問題(+1)的正確答案。我給出的答案不在圖片中,除非OP在循環之外放置聲明語句。 – Bhushan