簡體   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