调用我的函数时需要列定义列表错误
我试图从这样的查询中构建一个现有的函数:
Select * from dblink(
'host=db01
user=admin
password=xxxx
dbname=rm',
'select name,scopes from services es') as x(name varchar,scopes varchar)
union all
Select * from dblink(
'host=db02
user=admin
password=xxxx
dbname=rm',
'select name,scopes from services es') as x(name varchar,scopes varchar);
此查询有效,现在我想创建一个可重用的函数来跨此连接进行其他查询。 因此,我想在一个参数中传递选择,并在第二个参数中传递具有数据类型的别名。
这就是我的函数看起来像 atm 的样子。
CREATE OR REPLACE FUNCTION custom.dblink_function(qr varchar, alias varchar)
RETURNS TABLE(name character varying, scopes character varying)
LANGUAGE plpgsql
AS $$
begin
return query
Select * from dblink(
'host=db01
user=admin
password=xxxx
dbname=rm',
qr) as x(alias)
union all
Select * from dblink(
'host=db02
user=admin
password=xxxx
dbname=rm',
qr) as x(alias);
end;
$$
当我这样调用它时,出现以下错误。
select * from custom.dblink_function('''select name,scopes from services es''','name varchar,scopes varchar')
**
SQL Error [42601]: ERROR: a column definition list is required for functions returning "record"
Where: PL/pgSQL function custom.dblink_function(character varying,character varying) line 10 at RETURN QUERY
**
我做错了什么?
Im trying to build a function on hand from a query like this:
Select * from dblink(
'host=db01
user=admin
password=xxxx
dbname=rm',
'select name,scopes from services es') as x(name varchar,scopes varchar)
union all
Select * from dblink(
'host=db02
user=admin
password=xxxx
dbname=rm',
'select name,scopes from services es') as x(name varchar,scopes varchar);
This Query works and now i want to create a reusable function to make other queries across this connections.
Therefor i want to pass the select in one parameter and the alias with datatype in the second parameter.
This is how my Function looks like atm.
CREATE OR REPLACE FUNCTION custom.dblink_function(qr varchar, alias varchar)
RETURNS TABLE(name character varying, scopes character varying)
LANGUAGE plpgsql
AS $
begin
return query
Select * from dblink(
'host=db01
user=admin
password=xxxx
dbname=rm',
qr) as x(alias)
union all
Select * from dblink(
'host=db02
user=admin
password=xxxx
dbname=rm',
qr) as x(alias);
end;
$
When i call it like this i get the following error.
select * from custom.dblink_function('''select name,scopes from services es''','name varchar,scopes varchar')
**
SQL Error [42601]: ERROR: a column definition list is required for functions returning "record"
Where: PL/pgSQL function custom.dblink_function(character varying,character varying) line 10 at RETURN QUERY
**
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“alias”是一个varchar,而不是一个列定义列表(据我所知没有类型)。它可能包含可以解释为列列表的文本,但要使其正常工作,您需要将查询文本组合成一个字符串,然后使用
return queryexecute
来执行该字符串。你需要类似的东西
"alias" is a varchar, not a column definition list (which as far as I know has no type). It may contain text that can be interpreted as a column list, but for that to work you would need to assemble the query text together into a string, and then use
return query execute
to execute the string.You would need something like