(Oracle)如何方便地测试用户功能而无需创建另一个表

时间:2019-06-17 08:37:56

标签: oracle function

为了测试下面的功能“ test2”,我已经尝试过这些。

SELECT test2('A') FROM DUAL;SELECT test2('C') FROM DUAL;SELECT test2('E') FROM DUAL;

但是有一种方便的方法可以立即执行此操作吗? (无需制作其他表格)

我猜查询可能看起来像这样 选择test2(p.c1)FROM(....)p?

表和功能如下

    CREATE TABLE T2 (
    C1 VARCHAR2(1),
    C2 NUMBER
    );

    INSERT INTO T2  VALUES ('A',1);
    INSERT INTO T2  VALUES ('B',4);
    INSERT INTO T2  VALUES ('C',3);
    INSERT INTO T2  VALUES ('D',2);
    INSERT INTO T2  VALUES ('E',4);

    CREATE OR REPLACE FUNCTION test2
    (p1 IN VARCHAR2)
    RETURN NUMBER AS V_VALUE NUMBER;
    BEGIN
    SELECT(
        SELECT C2 
        FROM T2

    WHERE C1=p1)
INTO V_VALUE
FROM DUAL;
RETURN V_VALUE;
END;
/

1 个答案:

答案 0 :(得分:0)

我认为您可能会追求以下目标:

WITH test_data AS (SELECT 'A' val FROM dual UNION ALL
                   SELECT 'B' val FROM dual UNION ALL
                   SELECT 'C' val FROM dual UNION ALL
                   SELECT 'D' val FROM dual UNION ALL
                   SELECT 'E' val FROM dual)
SELECT test2(val)
FROM   test_data;

使用select ... from dual union all select ... from dual ...是为简单测试设置临时测试数据的好方法,而无需创建额外的对象来保存数据。