简体   繁体   English

Mysql-创建存储过程

[英]Mysql - Creating Stored Procedure

Using Ver 14.12 Distrib 5.0.45 (I know it's old), 使用Ver 14.12 Distrib 5.0.45 (我知道它很旧),

file: storedprocedure.sql contains: 文件: storedprocedure.sql包含:

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

I've tried both executing this command-line with: 我试过使用以下命令执行此命令行:

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

and from within 并从内部

mysql -u root -p -h localhost DB

mysql> *copied the code in from storedprocedure.sql

The error I get is: 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 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

However, another question on StackOverflow uses this exact syntax and it worked for everyone else? 但是,有关StackOverflow的另一个问题使用此确切语法,是否对其他所有人有用?

You were using a reserved word table . 您正在使用保留字table Try the following. 请尝试以下方法。 Works fine as tested. 经测试工作正常。 If you need to use an if-y word like a Reserved Word, then surround it with back-ticks. 如果您需要使用诸如保留字之类的if-y字,请用反引号将其引起来。 This includes words for column names and tables that include a space or a hyphen. 这包括列名称的单词和包含空格或连字符的表。

schema: 模式:

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');

Stored proc: 存储过程:

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 ;

test: 测试:

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

MySQL Reserved Words ... the ones with an (R). MySQL 保留字 ...带有(R)的字。

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

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