MYSQL'1064'中的语法错误

时间:2013-12-17 06:22:21

标签: php mysql sql stored-procedures

我无法弄清楚以下查询的语法错误..帮助?救命 ?帮忙?

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 

3 个答案:

答案 0 :(得分:3)

尝试将DECLARE statements放在BEGIN之后:

  

DECLARE仅允许在BEGIN ... END复合语句中使用,并且必须在其开始之前,在任何其他语句之前。

答案 1 :(得分:1)

COUNT(suffixPrefixId)FROM 分隔为 COUNT(suffixPrefixId)FROM

答案 2 :(得分:1)

尝试从if语句中删除分号:

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