I have two version of the same store procedure:
1)The first one is using implicit declaration and is working as expected if I go to workbench.
DROP procedure IF EXISTS `Elmah_GetErrorXml`;
DELIMITER $$
CREATE PROCEDURE `Elmah_GetErrorXml` (IN `pApplication` NVARCHAR(60), IN `pPageIndex` INT, IN `pPageSize` INT, OUT `pTotalCount` INT)
BEGIN
SELECT COUNT(*) INTO `pTotalCount` FROM `Elmah_Error` WHERE `Application`= pApplication;
SET @startRowIndex = pPageIndex * (pPageSize + 1);
SET @page_Count = pPageSize;
PREPARE STMT FROM 'SELECT * FROM `elmah_error` WHERE `Application`=Application ORDER BY `TimeUtc` DESC, `Sequence` DESC LIMIT ?,?';
EXECUTE STMT USING @startRowIndex, @page_Count;
END$$
DELIMITER $$
2) the second one is trying to use explicit declaration but when I try run it in to workbech i got some errors:
DROP procedure IF EXISTS `Elmah_GetErrorXml`;
DELIMITER $$
CREATE PROCEDURE `Elmah_GetErrorXml` (IN `pApplication` NVARCHAR(60), IN `pPageIndex` INT, IN `pPageSize` INT, OUT `pTotalCount` INT)
BEGIN
DECLARE startRowIndex INT DEFAULT 0;
DECLARE page_Count INT DEFAULT 0;
SELECT COUNT(*) INTO `pTotalCount` FROM `Elmah_Error` WHERE `Application`= pApplication;
SET startRowIndex = pPageIndex * (pPageSize + 1);
SET page_Count = pPageSize;
PREPARE STMT FROM 'SELECT * FROM `elmah_error` WHERE `Application`=Application ORDER BY `TimeUtc` DESC, `Sequence` DESC LIMIT ?,?';
EXECUTE STMT USING startRowIndex, page_Count;
END$$
DELIMITER $$
The errors are: Syntax error: unexpected 'startRowIndex' (identifier) Syntax error: unexpected page_Count (identifier)
I would like to know that should be the correct syntaxis in case to use explicit declaration. Any suggestion?
Note 1: I have readed the post from How to declare a variable in MySQL? but i can't see the problem with the version 2 of the stored procedure.
Note 2: if someone ask why I am not using the version 1 of the stored procedure is because my C# installer is throwing other error message: "MySql.Data.MySqlClient.MySqlException : Parameter '@startRowIndex' must be defined."
UPDATE: the reason of the exception from sqlcommand is described here: Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?