执行准备声明MySQL不工作

时间:2013-01-18 10:43:22

标签: mysql execute prepare

我一直在尝试确定我在MySQL中创建的存储过程有什么问题。我在创建它时没有遇到任何问题,但是当我试图调用它时它不起作用。 Workbench显示的错误是

  

错误代码:1064。您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在“SELECT ApplicantID,LastName,FirstName,Mid”附近使用的语法   第4行。

这是我的存储过程,

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`pinpoint_kevin`@`%` PROCEDURE `usp_Processing_GetApplicantByJobPostIDandStatus`(
    IN _ApplicationStatus VARCHAR(100),
    IN _JobPostID VARCHAR(64),
    IN _startRowIndex INT,
    IN _maximumRows INT,
    IN _Order NVARCHAR(20))
BEGIN
    DECLARE _OuterSelectString VARCHAR(1000);
    DECLARE _OuterFromString1 VARCHAR(1000);
    DECLARE _OuterFromString2 VARCHAR(1000);
    DECLARE _InnerSelectString VARCHAR(2000);
    DECLARE _InnerFromString VARCHAR(1000);
    DECLARE _InnerWhereString VARCHAR(1000);
    DECLARE _InnerOrderByString VARCHAR(1000);
    DECLARE _OuterWhereString VARCHAR(1000);
    DECLARE _ProcessingStepID VARCHAR(64);
    DECLARE _ApplicationProcessingID VARCHAR(64);
    DECLARE _YEHEY TEXT;

    SELECT
        ProcessingStepID
    INTO
        _ProcessingStepID
    FROM
        ProcessingStep
    WHERE
        ProcessingStepName = _ApplicationStatus;

    SELECT
        ApplicationProcessingID
    INTO
        _ApplicationProcessingID
    FROM
        ApplicationProcessing AS AP
            INNER JOIN
        ProcessingStep AS PS ON AP.ProcessingStepID = PS.ProcessingStepID
    WHERE
        PS.ProcessingStepName = _ApplicationStatus
    ORDER BY
        AP.TransactionDate
    LIMIT
        1;

    SET
        _OuterSelectString = '
                                SET
                                    @row_num = 0;

                                SELECT
                                    ApplicantID,
                                    LastName,
                                    FirstName,
                                    MiddleName,
                                    Email,
                                    ContactNumber,
                                    ApplicantCount,
                                    ApplicationNumber,
                                    ResumeStatus,
                                    LastTransactionDate,
                                    MovedBy,
                                    StepType,
                                    AdministeredBy,
                                    Evaluation,
                                    Recommendation,
                                    DateTaken,
                                    Mark,
                                    RowRank
                             ';
    SET
        _OuterFromString1 = 'FROM (
                            ';

    SET
        _InnerSelectString = 'SELECT 
                                (SELECT
                                    COUNT(*)
                                FROM
                                    Applicant_2 AS App 
                                        INNER JOIN
                                    ApplicantJobPost AS AJP ON App.ApplicantID = AJP.ApplicantID
                                        INNER JOIN
                                    ApplicationStatus AS APPS ON AJP.ApplicationStatusID = APPS.ApplicationStatusID
                                WHERE
                                    APPS.ApplicationStatusName = ?
                                        AND
                                    AJP.JobPostId = ? AND AJP.IsForwarded = 0) AS ApplicantCount,
                                    App.ApplicantID AS ApplicantID,
                                    App.LastName AS LastName,
                                    App.FirstName AS FirstName,
                                    App.MiddleName AS MiddleName,
                                    App.Email AS Email,
                                    App.BirthDate AS BirthDate,
                                    App.Address AS Address,
                                    App.ContactNumber AS ContactNumber,
                                    App.FileName  AS FileName,
                                    App.FileExtension AS FileExtension,
                                    App.DateCreated AS DateCreated,
                                    AJP.ApplicationNumber AS ApplicationNumber,
                                    AJP.IsReadResume AS ResumeStatus,
                                    AP.TransactionDate AS LastTransactionDate,
                                    AP.MovedBy AS MovedBy,
                                    AP.ProcessingStepType AS StepType,
                                    AP.AdministeredBy AS AdministeredBy,
                                    AP.Evaluation AS Evaluation,
                                    AP.Recommendation AS Recommendation,
                                    AP.DateTaken AS DateTaken,
                                    AJP.Mark AS Mark,
                                    @row_num := @row_num + 1 AS RowRank 
                                    ';

    SET
        _InnerFromString = 'FROM
                                Applicant_2 AS App 
                                    INNER JOIN
                                ApplicantJobPost AS AJP ON App.ApplicantID = AJP.ApplicantID
                                    INNER JOIN
                                ApplicationStatus AS APPS ON AJP.ApplicationStatusID = APPS.ApplicationStatusID
                                    INNER JOIN
                                ApplicationProcessing AS AP ON AP.ApplicantJobPostID = AJP.ApplicantJobPostID
                            ';

    SET
        _InnerWhereString = 'WHERE
                                APPS.ApplicationStatusName = ?
                                    AND
                                AJP.JobPostId = ?
                                    AND
                                AP.ProcessingStepID = ?
                                    AND
                                AJP.IsForwarded = 0
                            ';

    IF (_ApplicationStatus = 'Exam' OR _ApplicationStatus = 'Interview')
    THEN
        SET _InnerWhereString = 'WHERE
                                APPS.ApplicationStatusName = ?
                                    AND
                                AJP.JobPostId = ?
                                    AND
                                AP.ProcessingStepID = ?
                                    AND
                                AJP.IsForwarded = 0
                                    AND
                                AP.ApplicationProcessingID = ? ';
    END IF;

    SET
        _OuterFromString2 = ') AS WithRowNumbers
                            ';

    SET
        _OuterWhereString = 'WHERE
                                RowRank > _startRowIndex AND RowRank <= (_startRowIndex + _maximumRows)
                            ';

    IF (_Order = 'Transaction')
    THEN
        SET
            _InnerOrderByString = 'ORDER BY 
                                        AP.TransactionDate,
                                        LastName,
                                        IsReadResume,
                                        Mark,
                                        DateTaken DESC
                                  ';

    ELSEIF (_Order = 'TransactionDesc')
    THEN
        SET
            _InnerOrderByString = 'ORDER BY
                                        AP.TransactionDate DESC,
                                        LastName,
                                        IsReadResume,
                                        Mark,
                                        DateTaken DESC
                                  ';

    ELSEIF (_Order = 'Last NameDesc')
    THEN
        SET
            _InnerOrderByString = 'ORDER BY
                                        LastName DESC,
                                        IsReadResume,
                                        Mark,
                                        AP.TransactionDate DESC,
                                        DateTaken DESC
                                  ';

    ELSEIF (_Order = 'Last Name')
    THEN
        SET
            _InnerOrderByString = 'ORDER BY
                                        LastName,
                                        IsReadResume,
                                        Mark,
                                        AP.TransactionDate DESC,
                                        DateTaken DESC
                                  ';

    ELSEIF (_Order = 'Resume StatusDesc')
    THEN
        SET
            _InnerOrderByString = 'ORDER BY
                                        IsReadResume DESC,
                                        Mark,
                                        AP.TransactionDate DESC,
                                        LastName,
                                        DateTaken DESC
                                  ';

    ELSEIF (_Order = 'Resume Status')
    THEN
        SET
            _InnerOrderByString = 'ORDER BY
                                        IsReadResume,
                                        Mark,
                                        AP.TransactionDate DESC,
                                        LastName,
                                        DateTaken DESC
                                  ';
    END IF;

    SET _YEHEY = CONCAT(_OuterSelectString, _OuterFromString1, _InnerSelectString, _InnerFromString, _InnerWhereString, _OuterFromString2, _OuterWhereString);
    SET @WholeQuery = _YEHEY;
    PREPARE statement FROM @WholeQuery;
    SET @a = _ApplicationStatus;
    SET @b = _JobPostID;
    SET @c = _ApplicationStatus;
    SET @d = _JobPostID;
    SET @e = _ProcessingStepID;

    IF (_ApplicationStatus = 'Exam' OR _ApplicationStatus = 'Interview')
    THEN
        SET @f = _ApplicationProcessingID;
        EXECUTE statement USING @a, @b, @c, @d, @e, @f;

    ELSE
        EXECUTE statement USING @a, @b, @c, @d, @e;
    END IF;

    DEALLOCATE PREPARE statement;

    SELECT _YEHEY;
    SELECT @WholeQuery;

END

我已经尝试复制生成的字符串/查询并在另一个窗口中运行它并替换变量的相应值并且它正在工作。

我坚持使用这个存储过程。我希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:2)

正如SQL Syntax for Prepared Statements所述:

  

预处理语句的SQL语法不支持多语句(即,单个字符串中的多个语句由“;”字符分隔)。