以下代码中的错误似乎是啥?
Posted
技术标签:
【中文标题】以下代码中的错误似乎是啥?【英文标题】:What seems to be the error here in the below code?以下代码中的错误似乎是什么? 【发布时间】:2021-04-12 17:42:52 【问题描述】:错误代码:2014 命令不同步;你现在不能运行这个命令
平台 - My SQL Workbench
DELIMITER//
DROP PROCEDURE IF EXISTS must_watch_movies;
CREATE PROCEDURE
must_watch_movies ()
BEGIN DECLARE mTitle
VARCHAR (45);
DECLARE mDistributor VARCHAR (45);
DECLARE mRelease datetime;
DECLARE result VARCHAR (1000);
DECLARE no_records INTEGER DEFAULT FALSE;
DECLARE cursor_movies CURSOR FOR
SELECT title, Distributor, year (release_date)
FROM movies WHERE gross > 200000000 ORDER BY title;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET SET no_records = TRUE;
OPEN cursor_movies;
WHILE no_records = FALSE DO
FETCH cursor_movies INTO mTitle, mDistributor, mRelease;
SET result =
CONCAT ("'", mTitle, "','" mDistributor, "','" mRelease, "|");
END WHILE;
CLOSE cursor_movies;
SELECT result AS "Output";
END//
DELIMITER;
调用 must_watch_movies();
声明的值与表中实际列值的数据类型相匹配。 比如mTitle和title是同一个类型
【问题讨论】:
【参考方案1】:您的代码中有一些错误
在处理处理程序时使用双重 SET
如果你想要所有电影,你必须连接结果。
输出一定要复杂一些,但我把它留给你
CREATE TABLE movies (title varchar(10),Distributor varchar(19), release_date date,gross BIGINT)
INSERT INTO movies VALUES('text1','text2', NOW(),200000001),('text3','text4', NOW(),200000001),('text5','text6', NOW(),200000001)
CREATE PROCEDURE must_watch_movies () BEGIN DECLARE mTitle VARCHAR (45); DECLARE mDistributor VARCHAR (45); DECLARE mRelease INT; DECLARE result VARCHAR (1000) DEFAULT ""; DECLARE no_records INTEGER DEFAULT FALSE; DECLARE cursor_movies CURSOR FOR SELECT title, Distributor, year (release_date) FROM movies WHERE gross > 200000000 ORDER BY title; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_records = TRUE; OPEN cursor_movies; WHILE no_records = FALSE DO FETCH cursor_movies INTO mTitle, mDistributor, mRelease; SET result = CONCAT (result,"'", mTitle, "','", mDistributor, "','", mRelease, "|"); END WHILE; CLOSE cursor_movies; SELECT result AS "Output"; END
|输出 | | :------------------------------------------------- -------------------------------------------------- | | 'text1','text2','2021|'text3','text4','2021|'text5','text6','2021|'text5','text6','2021| | ✓CALL must_watch_movies()
db小提琴here
【讨论】:
谢谢!我让它运行,但我没有得到任何输出,或者我应该说我得到 null 当您看到该程序有效时,请更改 dbfiddle 并发布一个 lonk ,以便我可以检查它并请复制所有内容,尤其是结果的默认值 谢谢。事实证明,百万是 1 对 100 万,2 对 200 万,依此类推。终于成功了 @albion 请不要忘记accept it以上是关于以下代码中的错误似乎是啥?的主要内容,如果未能解决你的问题,请参考以下文章