代码之家  ›  专栏  ›  技术社区  ›  uma

如何将此查询转换为过程?

  •  0
  • uma  · 技术社区  · 7 年前

    我写了下面的选择查询和它的工作查找,并给出了输出。

        Select custname,contactno, enc_dec.decrypt(creditcardno,password) as  
    creditcardno ,enc_dec.decrypt(income,password) as 
    income from employees where custid=5;
    

    enter image description here

    我这样写程序,它遵守了,但当调用它时,并没有打印结果和给定的错误。

        CREATE OR REPLACE  PROCEDURE retrieve_decrypt(
        custid  in NUMBER,
        decrypt_value out sys_refcursor
        ) 
        IS
       BEGIN
         open decrypt_value for Select custname,contactno, enc_dec.decrypt(creditcardno,password) as  
           creditcardno ,enc_dec.decrypt(income,password) as 
                income  from employees where custid=custid  ;
         COMMIT;
       END;
    /
    

    我这样叫它 SELECT retrieve_decrypt(5) FROM DUAL; . enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   Alex Poole    7 年前

    您创建了一个过程,而不是函数,因此无法从SQL语句中调用它。参数也与定义不匹配。

    variable rc refcursor;
    execute retrieve_decrypt(5, :rc);
    print rc
    

    然后将这三行作为脚本运行。

    rc 当用作过程参数时。还要注意的是 variable , execute print 都是客户端命令。和 只是匿名PL/SQL块的简写。

    该过程更通用的用法是使用局部refcursor变量从PL/SQL块调用它,并在结果集上循环,对每一行执行操作。不过,你想用它们做什么还不清楚。

    也可以将过程转换为函数并返回refcursor,而不是将其作为out参数:

    CREATE OR REPLACE FUNCTION retrieve_decrypt(
        custid  in NUMBER
    )
    RETURN sys_refcursor
    IS
        decrypt_value sys_refcursor;
    BEGIN
         open decrypt_value for Select custname,contactno, enc_dec.decrypt(creditcardno,password) as  
           creditcardno ,enc_dec.decrypt(income,password) as 
                income  from employees where custid=custid  ;
        RETURN decrypt_value;
    END;
    /
    

    (未经测试)然后你可以称之为:

    SELECT retrieve_decrypt(5) FROM DUAL;
    

    但并不是所有的客户端都会整齐地显示结果。您还可以从PL/SQL块调用并迭代结果。

    custid 如果是唯一的,则结果集将是单个值,因此可以使用标量变量和out参数。但目前尚不清楚情况是否如此。