MySql 查询语法错误

发布于 2024-11-05 04:13:39 字数 1177 浏览 3 评论 0原文

我正在尝试通过 C# 创建一个 MySql 存储过程。我也创建了其他一些,但是当我尝试通过 MySqlCommand.ExecuteNonQuery 执行查询时,它会抛出一个异常,表明您的查询语法不兼容。这是异常消息:

您的 SQL 语法有错误; 检查对应的手册 您的 MySQL 服务器版本 在 'SET 附近使用正确的语法 变量记录存在 = (SELECT COUNT(*) 来自 SmartCache_Sync,其中 MachineNa' 在第 10 行

这里我试图构建查询字符串:

string sql = @"CREATE PROCEDURE SmartCache_UpdateSync
(
VariableMachineName varchar(50)
)
BEGIN
    DECLARE VariableRecordExists int;
    DECLARE VariableSetDate datetime;
    START TRANSACTION;
        SET VariableSetDate= Now()
        SET VariableRecordExists = (SELECT COUNT(*) FROM SmartCache_Sync WHERE MachineName = VariableMachineName)
        IF VariableRecordExists = 1
        THEN
            UPDATE SmartCache_Sync 
            SET LastUpdate = VariableSetDate
            WHERE MachineName= VariableMachineName;
        ELSE
            INSERT INTO SmartCache_Sync 
            (MachineName,LastUpdate)
            VALUES (VariableMachineName,VariableSetDate);
        END IF;
    COMMIT; 

    SELECT VariableSetDate;
END";

我不知道我在哪里犯了错误。可能我在某处缺少一个分号 ; 或什么。如果有人能帮助我,我将不胜感激。

I am trying to create a MySql stored procedure through C#. I have created some other as well but when I try to execute the Query via MySqlCommand.ExecuteNonQuery its throws a exception that you query syntax is not compatible. Here is the exception message:

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 'SET
VariableRecordExists = (SELECT
COUNT(*) FROM SmartCache_Sync WHERE
MachineNa' at line 10

And here I am trying to build the query string:

string sql = @"CREATE PROCEDURE SmartCache_UpdateSync
(
VariableMachineName varchar(50)
)
BEGIN
    DECLARE VariableRecordExists int;
    DECLARE VariableSetDate datetime;
    START TRANSACTION;
        SET VariableSetDate= Now()
        SET VariableRecordExists = (SELECT COUNT(*) FROM SmartCache_Sync WHERE MachineName = VariableMachineName)
        IF VariableRecordExists = 1
        THEN
            UPDATE SmartCache_Sync 
            SET LastUpdate = VariableSetDate
            WHERE MachineName= VariableMachineName;
        ELSE
            INSERT INTO SmartCache_Sync 
            (MachineName,LastUpdate)
            VALUES (VariableMachineName,VariableSetDate);
        END IF;
    COMMIT; 

    SELECT VariableSetDate;
END";

I don't know where I am making a mistake. Probably I am missing a semi colon ; somewhere or what. I would be obliged if anyone could help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半透明的墙 2024-11-12 04:13:39

我不知道如何在 C# 中进行查询,但通常 ; 是查询的结尾:因此,如果您不更改 delimiter,那么您将结束查询早指挥。通常你会做这样的事情:

delimiter //

CREATE PROCEDURE simpleproc (OUT param1 INT)
 BEGIN
  SELECT COUNT(*) INTO param1 FROM t;
 END//

delimiter ;

来自:
http://dev.mysql.com/doc/refman/5.0 /en/create-procedure.html

I don't know about making queryies in c#, but normally the ; is the end of your query: so if you don't change the delimiter, you are ending your command early. normally you would do something like this:

delimiter //

CREATE PROCEDURE simpleproc (OUT param1 INT)
 BEGIN
  SELECT COUNT(*) INTO param1 FROM t;
 END//

delimiter ;

from:
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

物价感观 2024-11-12 04:13:39

有点偏离主题 - 但是...

一般来说,当您想要做的只是检查是否存在任何行时,更喜欢“如果存在(选择...)”而不是“选择计数(*)...”。它比实际计算所有行要便宜得多。

其次,看起来好像您正在尝试执行“upsert”,这在 MySQL 中就是这样

INSERT INTO SmartCache_Sync(MachineName,LastUpdate)
    VALUES (VariableMachineName,VariableSetDate)
    ON DUPLICATE KEY UPDATE LastUpdate = VariableSetDate

,然后您也不需要显式事务。

当然,这假设 MachineName 是主键,我猜它是主键。

Slightly off-topic - but...

In general, prefer "if exists (select...)" to "select count(*)..." when all you want to do is check if any rows exist. It is far cheaper than actually counting all the rows.

And secondly, it looks as though you are trying to do an "upsert", which in MySQL would be

INSERT INTO SmartCache_Sync(MachineName,LastUpdate)
    VALUES (VariableMachineName,VariableSetDate)
    ON DUPLICATE KEY UPDATE LastUpdate = VariableSetDate

and then you don't need the explicit transaction either.

This of course assumes that MachineName is a primary key, which I'm guessing it is.

小女人ら 2024-11-12 04:13:39

我的猜测是正确的,我缺少一个“;”。老实说,我花了两个小时才弄清楚。

**

SET VariableSetDate= Now();
SET VariableRecordExists = (SELECT COUNT(*) FROM SmartCache_Sync
WHERE 机器名称 =
变量机器名);

**

两个语句都没有“;”在最后

My guess was right I was missing a ";". And honestly speaking it took me 2 hours to that out.

**

SET VariableSetDate= Now();
SET VariableRecordExists = (SELECT COUNT(*) FROM SmartCache_Sync
WHERE MachineName =
VariableMachineName);

**

Both the statements didn't have ";" at the end

べ映画 2024-11-12 04:13:39
CREATE PROCEDURE SmartCache_UpdateSync
 (
    VariableMachineName varchar(50)
)
    BEGIN
        DECLARE VariableRecordExists int;
        DECLARE VariableSetDate datetime;
        START TRANSACTION;
            SET VariableSetDate= Now()            

(SELECT VariableRecordExists = COUNT(*) FROM SmartCache_Sync WHERE MachineName = VariableMachineName)


            IF VariableRecordExists = 1
            THEN
                UPDATE SmartCache_Sync 
                SET LastUpdate = VariableSetDate
                WHERE MachineName= VariableMachineName;
            ELSE
                INSERT INTO SmartCache_Sync 
                (MachineName,LastUpdate)
                VALUES (VariableMachineName,VariableSetDate);
            END IF;
        COMMIT; 

        SELECT VariableSetDate;
    END";
CREATE PROCEDURE SmartCache_UpdateSync
 (
    VariableMachineName varchar(50)
)
    BEGIN
        DECLARE VariableRecordExists int;
        DECLARE VariableSetDate datetime;
        START TRANSACTION;
            SET VariableSetDate= Now()            

(SELECT VariableRecordExists = COUNT(*) FROM SmartCache_Sync WHERE MachineName = VariableMachineName)


            IF VariableRecordExists = 1
            THEN
                UPDATE SmartCache_Sync 
                SET LastUpdate = VariableSetDate
                WHERE MachineName= VariableMachineName;
            ELSE
                INSERT INTO SmartCache_Sync 
                (MachineName,LastUpdate)
                VALUES (VariableMachineName,VariableSetDate);
            END IF;
        COMMIT; 

        SELECT VariableSetDate;
    END";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文