PL/SQL传递函数作为参数 [英] PL/SQL passing functions as parameters

查看:124
本文介绍了PL/SQL传递函数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用PL/SQL编写了半年的程序,给人的印象是它是一种非常普通的编程语言(IMHO).尽管我偶然发现了一些有趣的文章,例如这篇文章-

I've programmed in PL/SQL during half an year and I had the impression it's a quite plain programming language (IMHO). Although I've stumbled upon interesting articles, like this one - Design Patterns in PL/SQL – Interface Injection for even looser coupling, I recommend reading. Talking about dependency injection, I miss an special feature: passing subroutines as parameters. Is it possible? How?

例如,假设我在javascript中有这样的代码:

For instance, imagine I have a code like this in javascript:

function tell_me (printer) {
  printer ("hello");
}

tell_me (function () {
  console.log (word);
});

是否可以在PL/SQL中执行类似的操作?

Is it possible to do something similar in PL/SQL?

推荐答案

您不能直接将函数作为参数传递.最好的办法是使用动态PL/SQL执行以字符串形式传入的函数.我不建议这样做.在少数情况下,我可以看到使用动态PL/SQL,但这使您面临各种问题.

You can't pass a function as a parameter directly. The best you could do is use dynamic PL/SQL to execute a function passed in as a string. I do not recommend this. I can see the use of dynamic PL/SQL in a few cases, but this opens you up to all sorts of problems.

DECLARE 
   PROCEDURE inner_function
   IS
   BEGIN
       dbms_output.put_line('Output');
   END;

   PROCEDURE tell_me(parm_function varchar2) 
   IS
   BEGIN
       EXECUTE IMMEDIATE 'BEGIN '||parm_function||'(); END;';
   END;

BEGIN
   tell_me('inner_function');
END;

DBMS_OUTPUT在缓冲区中应该只包含输出".

DBMS_OUTPUT should just have "Output" in the buffer.

这可能不起作用,因为inner_function可能不在范围内.在这种情况下,请在架构本身中定义过程.

This may not work since inner_function may be out of scope. In that case, define the procedure in the schema itself.

这篇关于PL/SQL传递函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆