从嵌套块内访问外部块变量
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档 plpgsql结构显示您如何做到这一点。使用您的示例作为起点:
与您的示例的差异:
'test'而不是“测试”,因此该值不被视为标识符。
使用
<<< exoutblock>>
标签语法标记外部块。$< some_tag> $
不执行此操作。在
outerblock.test_variable
中使用标签从外部块中获取变量值。仅供参考,在您的原始示例中,不需要内部块,因为您不做任何更改值
test_variable
的任何事情,因此无论如何它将与外部块相同。Update
刚刚意识到,您可能会根据示例中使用
do
来尝试执行嵌套功能。那是另一回事。为了传递数据,您必须将其作为参数添加到嵌套函数中的参数中。由于do
函数不能具有无法正常工作的参数。您必须使用常规功能。进一步更新
您可以拥有一个嵌套
do
函数:您只是无法访问外部部分中的变量。它们必须作为函数参数的参数传递到内部函数中,并且
do
函数没有参数。您可以使用常规功能来完成。The documentation plpgsql structure shows you how to do this. Using your example as starting point:
Differences from your example:
'test' instead of "test" so the value is not seen as an identifier.
Using the
<<outerblock>>
label syntax to label the outer block. The$<some_tag>$
does not do this.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. SinceDO
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: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.