Search code examples
mysqlexecuteprepare

EXECUTE PREPARE STATEMENT MySQL NOT WORKING


I've been trying to determine what's the problem with the stored procedure that I made in MySQL. I've had no problems in creating it but it's not working when I am trying to call it. The error that the Workbench is displaying is

Error Code: 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 'SELECT ApplicantID, LastName, FirstName, Mid' at line 4.

And here is my stored procedure,

-- --------------------------------------------------------------------------------
-- 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

I've already tried copying the resulting string/query and run it in another window and replaced the appropriate values for the variables and it's working.

I'm stuck with this stored procedure. I hope you could help me. Thanks!


Solution

  • As documented under SQL Syntax for Prepared Statements:

    SQL syntax for prepared statements does not support multi-statements (that is, multiple statements within a single string separated by “;” characters).