繁体   English   中英

在选择中使用存储过程

[英]Use stored procedure in select

考虑一个返回表的存储过程:

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 ;

我现在想过滤返回表的结果:

SELECT animal, terrain FROM GetLegalAnimals('rodent') WHERE hit_points<42;

上面的查询无效,因为它不是有效的MySQL语法。 有什么方法可以执行这样的查询吗? 我不想为每个(无限)条件实现一个单独的GetLegalAnimals_*过程,并且我也不想为该过程添加其他参数,因为存在无限的SELECT和WHERE子句排列。

请注意,该存储过程使用了一个参数,因此,尽管SO社区提供了一些创造性的解决方案 ,但视图并不是合适的解决方案

如果要使用整个表,则需要一个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;
;

然后你可以查询

SELECT animal, terrain FROM GetLegalAnimals WHERE hit_points < 42;

使用view代替存储过程

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;

暂无
暂无

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

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