在 PostgreSQL 函数中的变量内执行查询
Executing query inside variable in PostgreSQL function
我是 postgres 的新手,对于一个场景,我将 SQL 语句存储在 table 中,对应于 table 名称。在函数中,我试图通过将 table 名称作为参数传递来从 table 获取查询来过滤它们。但是当我从变量执行查询时,它给出了错误
"SQL 错误 [42P01]:错误:关系“public.table_name”不存在
其中:PL/pgSQL function ops_data_refresh(text) line 45 at EXECUTE
execute format('select query from public.ops_dw_table_load where target_table=''%s'' and is_active =true',main_table)
into qry1;
if qry1 is not null then
raise notice '%',qry1;
execute qry1;
提高通知输出 insert into public.table_name select * from stage.table_name;
如果我手动 运行,我可以看到 table 中的查询,如果我手动 运行 它工作正常。但是当 运行ning 从函数中抛出上述错误时。
您的代码中存在 SQL 注入错误。应该是:
EXECUTE format('SELECT ... target_table = %L ...', main_table);
但问题出在第二个EXECUTE
:查询引用了一个不存在的table。更改查询或创建 table.
我是 postgres 的新手,对于一个场景,我将 SQL 语句存储在 table 中,对应于 table 名称。在函数中,我试图通过将 table 名称作为参数传递来从 table 获取查询来过滤它们。但是当我从变量执行查询时,它给出了错误
"SQL 错误 [42P01]:错误:关系“public.table_name”不存在 其中:PL/pgSQL function ops_data_refresh(text) line 45 at EXECUTE
execute format('select query from public.ops_dw_table_load where target_table=''%s'' and is_active =true',main_table)
into qry1;
if qry1 is not null then
raise notice '%',qry1;
execute qry1;
提高通知输出 insert into public.table_name select * from stage.table_name;
如果我手动 运行,我可以看到 table 中的查询,如果我手动 运行 它工作正常。但是当 运行ning 从函数中抛出上述错误时。
您的代码中存在 SQL 注入错误。应该是:
EXECUTE format('SELECT ... target_table = %L ...', main_table);
但问题出在第二个EXECUTE
:查询引用了一个不存在的table。更改查询或创建 table.