从嵌套块内访问外部块变量

发布于 2025-01-21 20:28:57 字数 632 浏览 3 评论 0原文

PLPGSQL从嵌套块中从父块访问变量

DO $outer-block$
DECLARE
  test_variable text DEFAULT "test";
BEGIN

  DO $inner-block$
  BEGIN

    RAISE NOTICE '%',test_variable;

  END;
  $inner-block$;

END;
$outer-block$;

我正在尝试使用PostgreSQL的 在范围内,当内部块得到控制时,是否有任何方法可以从嵌套块内访问它?

(我检查了这个问题:外部范围变量来自PostgreSQL中的函数?,哪种类似,但是在这种情况下,它试图从Whitin a函数访问外部块变量,这是一个持久的数据库对象,并且它将以某种方式试图创建一个封闭,这不是我在这里所做的,因为我想从一次执行内部执行内部访问外部块的范围,这可能会有所作为,但我不知道它)

I am trying to access a variable from a parent block from within a nested block using postgresql's plpgsql as follows

DO $outer-block$
DECLARE
  test_variable text DEFAULT "test";
BEGIN

  DO $inner-block$
  BEGIN

    RAISE NOTICE '%',test_variable;

  END;
  $inner-block$;

END;
$outer-block$;

When I try to run this, it tells me that "the column test_variable doesn't exist", so I suppose that this variable is not in scope when the inner block is given control, is there any way to access it from within the nested block?

(I checked up this question: How to access outer scope variables from a function in PostgreSQL?, which was sort of similar, but in that case it's trying to access the outer block variable from whitin a function, which is a persistent database object, and it would in a way be trying to create a closure, that is not what I am doing here, since I want to access the outer block's scope from within a one-time execution inner block, which might make a difference, but I don't know it for certain)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-28 20:28:57

文档 plpgsql结构显示您如何做到这一点。使用您的示例作为起点:

DO
$
<<outerblock>>
DECLARE
  test_variable text DEFAULT 'test';
BEGIN
    RAISE NOTICE '%',test_variable;

    DECLARE
        test_variable text := 'inner test';
    BEGIN
        RAISE NOTICE '%',test_variable;
        RAISE NOTICE '%', outerblock.test_variable;

  END;

END;
$;

NOTICE:  test
NOTICE:  inner test
NOTICE:  test

与您的示例的差异:

  1. 'test'而不是“测试”,因此该值不被视为标识符。

  2. 使用&lt;&lt;&lt; exoutblock&gt;&gt;标签语法标记外部块。 $&lt; some_tag&gt; $不执行此操作。

  3. outerblock.test_variable中使用标签从外部块中获取变量值。

仅供参考,在您的原始示例中,不需要内部块,因为您不做任何更改值test_variable的任何事情,因此无论如何它将与外部块相同。

Update

刚刚意识到,您可能会根据示例中使用do来尝试执行嵌套功能。那是另一回事。为了传递数据,您必须将其作为参数添加到嵌套函数中的参数中。由于do函数不能具有无法正常工作的参数。您必须使用常规功能。

进一步更新

您可以拥有一个嵌套do函数:

DO
$
<<outerblock>>
DECLARE
  test_variable text DEFAULT 'test';
BEGIN
    RAISE NOTICE '%',test_variable;

    DO
    $i$
    DECLARE
        inner_test_variable text := 'inner test';
    BEGIN
        RAISE NOTICE 'Inner %', inner_test_variable;

    END;
    $i$;
END;
$;

NOTICE:  test
NOTICE:  Inner inner test
DO

您只是无法访问外部部分中的变量。它们必须作为函数参数的参数传递到内部函数中,并且do函数没有参数。您可以使用常规功能来完成。

The documentation plpgsql structure shows you how to do this. Using your example as starting point:

DO
$
<<outerblock>>
DECLARE
  test_variable text DEFAULT 'test';
BEGIN
    RAISE NOTICE '%',test_variable;

    DECLARE
        test_variable text := 'inner test';
    BEGIN
        RAISE NOTICE '%',test_variable;
        RAISE NOTICE '%', outerblock.test_variable;

  END;

END;
$;

NOTICE:  test
NOTICE:  inner test
NOTICE:  test

Differences from your example:

  1. 'test' instead of "test" so the value is not seen as an identifier.

  2. Using the <<outerblock>> label syntax to label the outer block. The $<some_tag>$ does not do this.

  3. Using the label in outerblock.test_variable to fetch the variable value from the outer block.

FYI, in your original example there is no need for the inner block as you don't do anything that changes the value test_variable so it will be the same as the outer block anyway.

UPDATE

Just realized you maybe trying to do nested functions, per the use of DO in your example. That is a different thing then blocks. In order to pass data you would have to add it as a parameter to an argument in the nested function. Since DO functions can't have arguments that is not going to work. You would have to use a regular function.

FURTHER UPDATE

You can have a nested DO function:

DO
$
<<outerblock>>
DECLARE
  test_variable text DEFAULT 'test';
BEGIN
    RAISE NOTICE '%',test_variable;

    DO
    $i$
    DECLARE
        inner_test_variable text := 'inner test';
    BEGIN
        RAISE NOTICE 'Inner %', inner_test_variable;

    END;
    $i$;
END;
$;

NOTICE:  test
NOTICE:  Inner inner test
DO

You just can't access the variables in the outer portion. They would have to be passed into the inner function as a parameter to a function argument and DO functions do not have arguments. You could do it with a regular function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文