从列运行查询

时间:2013-11-02 12:20:26

标签: sql plsql

我想要的是:我有一个包含查询的表,我需要创建一个运行该列中某个值的查询

即:

queryname    query
Chips        select * from chips_table c join all_stores s on c.id=s.id where s.loc>213...
...          ...

我知道我想要上述查询的结果,如何运行程序或脚本或视图或类似

getme(Chips)

这将运行

select * from chips_table c join all_stores s on c.id=s.id where s.loc>213...

1 个答案:

答案 0 :(得分:0)

取决于您之后要对结果做什么,但对于PL / SQL处理,您可以使用以下内容:

-- Function to open and return the cursor for the query based on query name
CREATE OR REPLACE FUNCTION get_cursor (in_queryname IN VARCHAR2)
RETURN SYS_REFCURSOR
IS
    c_query SYS_REFCURSOR;
    v_query queries.query%type;
BEGIN
    SELECT query INTO v_query FROM queries WHERE queryname = in_queryname;
    OPEN c_query FOR v_query;
    RETURN c_query;
END;

-- Example of use
DECLARE
    c_query SYS_REFCURSOR;
    some_variable INTEGER;
    another_variable INTEGER;
BEGIN
    LOOP
        FETCH c_query INTO some_variable, another_variable;
        EXIT WHEN c_query%NOTFOUND;
        -- Do someting!
    END LOOP;
    CLOSE c_query;
EXCEPTION WHEN no_data_found THEN
    NULL; -- Log the error!
END;