为什么SELECT COUNT(*)上的MySQL if语句不起作用?(Why does this MySQL if statement on SELECT COUNT(*) not work?)

我有一个.bat文件,它执行如下SQL文件。

目的是如果在表中找到记录,则不会执行任何操作,而如果表为空,则将插入一些记录。

BEGIN DECLARE rowCount INT; SELECT count(*) FROM `martin1` INTO rowCount; IF rowCount <= 5 THEN END IF; END;

但是当我执行它时,会出现错误。 我试图删除DECLARE ,但即使对于(IF SELECT COUNT(*)...>0) ,仍然存在错误。

错误是,

第1行的错误1064(42000):SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在第2行的“DECLARE rowCount INT”附近使用正确的语法

我该如何解决这个问题?

I have a .bat file which executes the SQL file as follows.

The aim is that if records are found in a table, nothing will be done, whereas if the table is empty, some records will be inserted.

BEGIN DECLARE rowCount INT; SELECT count(*) FROM `martin1` INTO rowCount; IF rowCount <= 5 THEN END IF; END;

But when I execute it, there is an error. I tried to delete the DECLARE, but even for (IF SELECT COUNT(*)...>0) there is still an error.

The error is,

ERROR 1064 (42000) at line 1: 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 'DECLARE rowCount INT' at line 2

How can I resolve this?

最满意答案

试试这种方式

BEGIN DECLARE rowCount INT; SELECT count(*) INTO rowCount FROM `martin1` IF rowCount <= 5 THEN END IF; END;

看看这个

try this way

BEGIN DECLARE rowCount INT; SELECT count(*) INTO rowCount FROM `martin1` IF rowCount <= 5 THEN END IF; END;

And have a look at this

为什么SELECT COUNT(*)上的MySQL if语句不起作用?(Why does this MySQL if statement on SELECT COUNT(*) not work?)

我有一个.bat文件,它执行如下SQL文件。

目的是如果在表中找到记录,则不会执行任何操作,而如果表为空,则将插入一些记录。

BEGIN DECLARE rowCount INT; SELECT count(*) FROM `martin1` INTO rowCount; IF rowCount <= 5 THEN END IF; END;

但是当我执行它时,会出现错误。 我试图删除DECLARE ,但即使对于(IF SELECT COUNT(*)...>0) ,仍然存在错误。

错误是,

第1行的错误1064(42000):SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在第2行的“DECLARE rowCount INT”附近使用正确的语法

我该如何解决这个问题?

I have a .bat file which executes the SQL file as follows.

The aim is that if records are found in a table, nothing will be done, whereas if the table is empty, some records will be inserted.

BEGIN DECLARE rowCount INT; SELECT count(*) FROM `martin1` INTO rowCount; IF rowCount <= 5 THEN END IF; END;

But when I execute it, there is an error. I tried to delete the DECLARE, but even for (IF SELECT COUNT(*)...>0) there is still an error.

The error is,

ERROR 1064 (42000) at line 1: 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 'DECLARE rowCount INT' at line 2

How can I resolve this?

最满意答案

试试这种方式

BEGIN DECLARE rowCount INT; SELECT count(*) INTO rowCount FROM `martin1` IF rowCount <= 5 THEN END IF; END;

看看这个

try this way

BEGIN DECLARE rowCount INT; SELECT count(*) INTO rowCount FROM `martin1` IF rowCount <= 5 THEN END IF; END;

And have a look at this