MySQL 查询语句错误

发布于 2024-10-10 18:31:46 字数 527 浏览 6 评论 0原文

我正在尝试熟悉 Mysql 语法。到目前为止我只使用过MSSQL。我下载了 MySQL 查询浏览器并安装了 MySQL 版本 5.1

我想在 MySQL 的结果集选项卡中运行这行代码,但我不断遇到以下错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“declare iCtr int”附近使用的正确语法

代码:

declare iCtr int;
set iCtr = 1;

while iCtr < 1000
begin
    insert into employee (emp_id,emp_first_name,emp_last_name,status_id)
      values (iCtr, 'firstName' + iCtr, 'lastName' + iCtr, 1)
      set iCtr = iCtr + 1;
end

我只想填充我的员工表,但我无法通过 MySQL 语法。

I am trying to acquaint myself on Mysql syntax. I only have used MSSQL so far. I downloaded the MySQL Query Browser and have installed the MySQL Version 5.1

I wanted to run this line of code in the resultset tab of MySQL but I keep on encountering below error

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 iCtr int' at line 1

Code:

declare iCtr int;
set iCtr = 1;

while iCtr < 1000
begin
    insert into employee (emp_id,emp_first_name,emp_last_name,status_id)
      values (iCtr, 'firstName' + iCtr, 'lastName' + iCtr, 1)
      set iCtr = iCtr + 1;
end

I just wanted to populate my employees table but I cannot get past the MySQL syntax.

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

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

发布评论

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

评论(2

阪姬 2024-10-17 18:31:46

来自: http://dev.mysql.com/doc/refman/5.0 /en/declare.html

DECLARE 只能在 BEGIN ... END 复合语句内使用,并且必须位于其开头、任何其他语句之前。

我想这可能是麻烦所在?

from: http://dev.mysql.com/doc/refman/5.0/en/declare.html

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

I think that might be the trouble?

温馨耳语 2024-10-17 18:31:46

尝试

DELIMITER $


DROP PROCEDURE IF EXISTS `procedurename`$

CREATE PROCEDURE `procedurename`()
BEGIN
        declare iCtr int;
set iCtr = 1;

while iCtr < 1000 do

    insert into employee (emp_id,emp_first_name,emp_last_name,status_id)
      values (iCtr, 'firstName' + iCtr, 'lastName' + iCtr, 1);
      set iCtr = iCtr + 1;
end while;

    END$

DELIMITER ;

try

DELIMITER $


DROP PROCEDURE IF EXISTS `procedurename`$

CREATE PROCEDURE `procedurename`()
BEGIN
        declare iCtr int;
set iCtr = 1;

while iCtr < 1000 do

    insert into employee (emp_id,emp_first_name,emp_last_name,status_id)
      values (iCtr, 'firstName' + iCtr, 'lastName' + iCtr, 1);
      set iCtr = iCtr + 1;
end while;

    END$

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