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