0

我真的希望有人可以帮助我。

我有一个 SQL Server 2014 数据库,用于我编写的桌面应用程序。在不得不扩展之后,我想转换为 MySQL 以通过 Internet 访问数据库。

我使用 MySQL WorkBench Migration Tool 将所有表和数据从 SQL Server 复制到 MySQL Server。唯一的问题是程序不会复制,所以不得不手动修改它们。

MySQL中的过程示例:

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

服务器创建了这些过程,所有不使用 IN 输入的过程都显示了它们必须显示的内容,但是用户输入的过程给了我一个错误:从 PhpMyAdmin SQL 调用时:错误

SQL查询:编辑编辑

设置 FOREIGN_KEY_CHECKS = ON;

MySQL 说:文档

2014 - 命令不同步;你现在不能运行这个命令

当我在 C# Winforms App 中运行该过程时,它只返回一个空结果集。

我用来调用该过程的代码:

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

当我将过程中的代码作为普通 SQL 查询运行时,它也会按预期返回数据。

我希望这是足够的信息,并希望这不是一个重复的问题,但我确实环顾四周,仍然没有找到任何帮助。

谢谢你

4

1 回答 1

0

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:

@ 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;
于 2017-06-23T00:51:48.270 回答