简体   繁体   English

MySQL调用存储过程错误

[英]MySQL Calling A Stored Procedure Error

I am really hoping someone can help me out. 我真的希望有人能帮助我。

I had a SQL Server 2014 database which I used for a desktop application I wrote. 我有一个SQL Server 2014数据库,用于编写的桌面应用程序。 After having to expand, I want to convert to MySQL to access the database over the internet. 扩展后,我想转换为MySQL以通过Internet访问数据库。

I user MySQL WorkBench Migration Tool to copy all the tables and data from the SQL Server to the MySQL Server. 我使用MySQL WorkBench Migration Tool将所有表和数据从SQL Server复制到MySQL Server。 The only problem is the procedures would not copy, so had to modify them manually. 唯一的问题是程序无法复制,因此必须手动对其进行修改。

Example of a Procedure in MySQL: MySQL中的过程示例:

DELIMITER $$
CREATE DEFINER=`johandre`@`%` PROCEDURE `sp_GetAllOrdersBySuburb`( IN `@SuburbID` VARCHAR(50) )
NO SQL
SELECT * from Orders  WHERE DeliverySuburb = @SuburbID$$
DELIMITER ;

The server created the procedures, and all procedures not using IN inputs do show what they must, but the procedures that user inputs give me an error: When calling from PhpMyAdmin SQL: Error 服务器创建了过程,所有未使用IN输入的过程均显示了必须执行的操作,但是用户输入的过程给了我一个错误:从PhpMyAdmin SQL调用时:错误

SQL query: Edit Edit SQL查询:编辑编辑

SET FOREIGN_KEY_CHECKS = ON; SET FOREIGN_KEY_CHECKS = ON;

MySQL said: Documentation MySQL说:文档

2014 - Commands out of sync; 2014-命令不同步; you can't run this command now 您现在不能运行此命令

And when I run the procedure in C# Winforms App, it just returns a empty result set. 当我在C#Winforms App中运行该过程时,它仅返回一个空结果集。

The code I used to call the procedure: 我用来调用该过程的代码:

SET @p0='1'; CALL `sp_GetAllOrdersBySuburb`(@p0);

When I run the code in the procedure as a normal SQL query, then it also returns the data as expected. 当我以常规SQL查询的形式运行过程中的代码时,它也会按预期返回数据。

I hope this is enough information, and hope this isn't a repeat question, but I did look around and still found no help. 我希望这是足够的信息,并且希望这不是重复的问题,但是我确实环顾四周,但仍然找不到帮助。

Thank You 谢谢

I think, your problem might be your delimiter when defining the procedure. 我认为,您的问题可能是定义程序时的定界符。 Also, when using backticks to define your param (otherwise @ won't be allowed), you need them when accessing the param, too: 另外,当使用反引号定义您的参数时(否则将不允许@ ),访问参数时也需要它们:

DELIMITER $$ /* make $$ the delimiter for actually executing something */

CREATE DEFINER=`johandre`@`%` PROCEDURE `sp_GetAllOrdersBySuburb`( IN `@SuburbID` VARCHAR(50) )
NO SQL
SELECT * from Orders  WHERE DeliverySuburb = `@SuburbID`; /* normal ; here, you just define your proc */

$$ /* now execute the definition */

DELIMITER ; /* reset the delimiter */

For the delimiters: 对于定界符:

You change your delimiter because you don't want any ; 您更改定界符是因为您不需要任何定界符; in your procedure being interpreted as an execution delimiter but being part of your procedure instead. 在您的过程中,它被解释为执行定界符,但成为过程的一部分。 After that, you want to execute the whole definition and then reset the delimiter. 之后,您要执行整个定义,然后重置定界符。

For @ in parameters: 对于@ in参数:

@ is a reserved character only for accessing global variables. @是保留字符, 用于访问全局变量。 If you really want to use @ in your param, you need backticks to make this work. 如果您真的想在参数中使用@ ,则需要反引号以使其起作用。 Backticks allow you to use white spaces, reserved words and even strange characters or those who have a special meaning in regular syntax and are not allowed in an identifier otherwise to be used within identifiers anyway. 反引号允许您使用空格,保留字甚至是奇怪的字符,或者使用常规语法具有特殊含义并且不允许在标识符中使用的字符,否则该标识符将始终用于标识符中。 However, you have to use backticks for a correct dereference as well then. 但是,您还必须使用反引号进行正确的取消引用。

`@identifier`

and

@identifier

resolve to different things. 解决不同的事情。 That means, you need 这意味着,您需要

SELECT * from Orders  WHERE DeliverySuburb = `@SuburbID`;

instead of 代替

 SELECT * from Orders  WHERE DeliverySuburb = @SuburbID;

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

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