简体   繁体   English

mysql存储过程中的变量声明

[英]Variables declaration in mysql stored procedure

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. 1)第一个是使用隐式声明,如果我去工作台,它会按预期工作。

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: 2)第二个尝试使用显式声明,但是当我尝试将它运行到工作台时,我遇到了一些错误:

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)错误是:语法错误:意外的'startRowIndex'(标识符)语法错误:意外的page_Count(标识符)

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?注 1:我已经阅读了如何在 MySQL 中声明变量? but i can't see the problem with the version 2 of the stored procedure.但我看不到存储过程版本 2 的问题。

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."注意 2:如果有人问我为什么不使用存储过程的第 1 版是因为我的 C# 安装程序抛出其他错误消息:“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?更新:此处描述了 sqlcommand 异常的原因: Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?

When I pasted your code into Workbench, it showed the error on this line:当我将您的代码粘贴到 Workbench 中时,它显示了这一行的错误:

EXECUTE STMT USING startRowIndex, page_Count;

According to the documentation :根据文档

A statement prepared in stored program context cannot refer to stored procedure or function parameters or local variables because they go out of scope when the program ends and would be unavailable were the statement to be executed later outside the program.在存储程序上下文中准备的语句不能引用存储过程或函数参数或局部变量,因为它们在程序结束时超出范围并且如果稍后在程序外执行该语句将不可用。 As a workaround, refer instead to user-defined variables, which also have session scope;作为一种解决方法,请改用用户定义的变量,这些变量也具有会话作用域; see Section 9.4, “User-Defined Variables” .请参阅第 9.4 节,“用户定义的变量”

So in other words, you can't pass local ( DECLARE d) variables to a prepared statement;因此,换句话说,您不能将本地 ( DECLARE d) 变量传递给准备好的语句; you can only pass session variables ( @ variables.)您只能传递会话变量( @变量。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM