繁体   English   中英

PL/pgSQL:如何获取变量/参数的名称? [postgresql]

[英]PL/pgSQL: How to get variable's / parameter's name? [postgresql]

CREATE OR REPLACE FUNCTION public.test_function(test_parameter varchar default 'test_value_1')
    RETURNS void AS
$func$
DECLARE
    test_variable varchar = 'test_value_2';
BEGIN
    raise notice '% = %', (???), test_parameter; -- expected result: test_parameter = test_value_1
    raise notice '% = %', (???), test_variable;  -- expected result: test_variable = test_value_2
    return;
END
$func$ LANGUAGE plpgsql;

SELECT * FROM public.test_function();

如何在 PostgreSQL (PL/pgSQL) 中获取变量/参数的名称?

要么将变量名作为模板的一部分,要么将其作为字符串传递:

CREATE OR REPLACE FUNCTION public.test_function(test_parameter varchar default 'test_value_1')
    RETURNS void AS
$func$
DECLARE
    test_variable varchar = 'test_value_2';
BEGIN
    raise notice '% = %', 'test_parameter', test_parameter; -- expected result: test_parameter = test_value_1
    raise notice 'test_variable = %', test_variable;  -- expected result: test_variable = test_value_2
    return;
END
$func$ LANGUAGE plpgsql;

SELECT * FROM public.test_function();

某些语言将函数参数作为字典对象传递(如 Python)。 Postgres 环境中的参数作为值数组传递(并且无法从 PLpgSQL 访问该数组)。 名称可以从系统表pg_proc ,但在 plpgsql 中读取这些数据很慢而且很重要。 在 PLpgSQL 中使用参数或变量的理念与静态编译语言 C、C++、Pascal、Ada 一样,......您无法在运行时获取函数内部的变量或参数列表。 此信息不存在于编译代码中,或者无法从语言 (PLpgSQL) 访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM