繁体   English   中英

Mysql-创建存储过程

[英]Mysql - Creating Stored Procedure

使用Ver 14.12 Distrib 5.0.45 (我知道它很旧),

文件: storedprocedure.sql包含:

DELIMITER //
CREATE PROCEDURE loadDashboard 
(
    IN limitStr int(11)
)
BEGIN
    SELECT table123.ID
    FROM table123
    ORDER BY date_lastmodified LIMIT limitStr;
END //
DELIMITER ;

我试过使用以下命令执行此命令行:

mysql -u root -p -h localhost DB < storedprocedure.sql

并从内部

mysql -u root -p -h localhost DB

mysql> *copied the code in from storedprocedure.sql

我得到的错误是: ERROR 1064 (42000) 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 'limitStr ERROR 1064 (42000) 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 'limitStr

但是,有关StackOverflow的另一个问题使用此确切语法,是否对其他所有人有用?

您正在使用保留字table 请尝试以下方法。 经测试工作正常。 如果您需要使用诸如保留字之类的if-y字,请用反引号将其引起来。 这包括列名称的单词和包含空格或连字符的表。

模式:

drop table if exists mytable123;
create table mytable123
(
    id int auto_increment primary key,
    date_lastmodified datetime not null
);

insert mytable123(date_lastmodified) values ('2006-07-23'),('2006-07-27 13:10:09');

存储过程:

drop procedure if exists loadDashboard;
DELIMITER //
CREATE PROCEDURE loadDashboard 
(
    IN limitStr int(11)
)
BEGIN
    DECLARE theLimit int;   -- to maintain compatibility (see comment from user wchiquito)

    set theLimit=limitStr;

    SELECT  ID
    FROM mytable123
    ORDER BY date_lastmodified 
    LIMIT theLimit; -- use the local variable for this
END //

DELIMITER ;

测试:

call loadDashboard(1);
call loadDashboard(17);

MySQL 保留字 ...带有(R)的字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM