MySql 查询语法错误
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道如何在 C# 中进行查询,但通常
;
是查询的结尾:因此,如果您不更改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 thedelimiter
, you are ending your command early. normally you would do something like this:from:
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
有点偏离主题 - 但是...
一般来说,当您想要做的只是检查是否存在任何行时,更喜欢“如果存在(选择...)”而不是“选择计数(*)...”。它比实际计算所有行要便宜得多。
其次,看起来好像您正在尝试执行“upsert”,这在 MySQL 中就是这样
,然后您也不需要显式事务。
当然,这假设 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
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.
我的猜测是正确的,我缺少一个“;”。老实说,我花了两个小时才弄清楚。
**
**
两个语句都没有“;”在最后
My guess was right I was missing a ";". And honestly speaking it took me 2 hours to that out.
**
**
Both the statements didn't have ";" at the end