Consider a stored procedure which returns a table:
DELIMITER //
CREATE PROCEDURE GetLegalAnimals (IN prop VARCHAR(128))
BEGIN
-- Lots of fiddling with temporary tables here
SELECT
animal, hit_points, terrain, weight, height, girth, attack, defence, quest
FROM
temp_animals;
END //
DELIMITER ;
I would now like to filter the results of the returned table:
SELECT animal, terrain FROM GetLegalAnimals('rodent') WHERE hit_points<42;
The above query doesn't run as it is not valid MySQL syntax. Is there any way to perform such a query? I don't want to implement a separate GetLegalAnimals_*
procedure for each of the (infinite) conditions, and I don't want to add additional parameters to the procedure as there are infinite SELECT and WHERE clause permutations.
Note that the stored procedure uses a parameter, so a view is not a suitable solution despite some creative solutions of the SO community.
If you want to use a whole table, you need a VIEW
. First create the view
USE `yourdatabase`;
CREATE OR REPLACE VIEW `GetLegalAnimals` AS
-- Lots of fiddling with temporary tables here
SELECT
animal, hit_points, terrain, weight, height, girth, attack, defence, quest
FROM
temp_animals;
;
Then you can query
SELECT animal, terrain FROM GetLegalAnimals WHERE hit_points < 42;
Use view
instead of stored procedure
CREATE VIEW view_name AS
-- Lots of fiddling with temporary tables here
SELECT
animal, hit_points, terrain, weight, height, girth, attack, defence, quest
FROM
temp_animals;
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.