I created the following simple procedure for my database class assignment
DROP PROCEDURE IF EXISTS cs431_movie_database.must_watch_movies;
DELIMITER //
CREATE PROCEDURE cs431_movie_database.must_watch_movies()
BEGIN
-- variables that stores the fetch data for each row
DECLARE movie_name VARCHAR(50);
DECLARE distributor VARCHAR(20);
DECLARE release_year INT;
-- for exception handling
DECLARE row_not_found TINYINT DEFAULT FALSE;
-- the final output string
DECLARE output VARCHAR(400) DEFAULT '';
-- a cursor that goes throw each row of the movies database
DECLARE movies_cursor CURSOR FOR
SELECT title, Distributor, YEAR(release_date) AS release_year
FROM cs431_movie_database.movies
WHERE gross > 2
ORDER BY title ASC;
-- handler when there is no more row found by the cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET row_not_found = TRUE;
OPEN movies_cursor;
-- put fetch data into procedure variables then concat them into the output string
WHILE row_not_found = FALSE DO
FETCH movies_cursor INTO movie_name, distributor, release_year;
SET output = CONCAT(output, '\'', movie_name, '\', \'',distributor, '\', \'', release_year, '\'| ');
END WHILE;
CLOSE movies_cursor;
-- gets the output
SELECT output;
END //
DELIMITER ;
Where my expected output when I call it would be something like
'movie name 1', 'distributor 1', 'release year for movie 1' | 'movie name 2', 'distributor 2', 'release year for movie 2' | 'movie name 3', ....
But instead, I got a null string came back to me, I'm not sure what went wrong with my code, please help
Like @Barmar offers in a comment:
SELECT GROUP_CONCAT(CONCAT('\'',
movie_name,
'\', \'',
distributor,
'\', \'',
YEAR(release_date),
'\'')
ORDER BY title ASC
SEPARATOR '| ')
FROM cs431_movie_database.movies
WHERE gross > 2;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.