调用我的函数时需要列定义列表错误

发布于 2025-01-11 12:54:27 字数 1939 浏览 0 评论 0原文

我试图从这样的查询中构建一个现有的函数:

        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 技术交流群。

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

发布评论

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

评论(1

放肆 2025-01-18 12:54:27

“alias”是一个varchar,而不是一个列定义列表(据我所知没有类型)。它可能包含可以解释为列列表的文本,但要使其正常工作,您需要将查询文本组合成一个字符串,然后使用 return queryexecute 来执行该字符串。

你需要类似的东西

CREATE OR REPLACE FUNCTION dblink_function(qr varchar, alias varchar)
 RETURNS TABLE(name character varying, scopes character varying)
 LANGUAGE plpgsql
AS $
begin 
        return query execute
        format ($R$Select * from dblink(
                            'host=db01
                            user=admin
                            password=xxxx
                            dbname=rm',
                            %L) as x(%s)
    union all
        Select * from dblink(
                            'host=db02
                            user=admin
                            password=xxxx
                            dbname=rm',
                            %L) as x(%s)$R$,qr,alias,qr,alias);end;
$;

"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

CREATE OR REPLACE FUNCTION dblink_function(qr varchar, alias varchar)
 RETURNS TABLE(name character varying, scopes character varying)
 LANGUAGE plpgsql
AS $
begin 
        return query execute
        format ($R$Select * from dblink(
                            'host=db01
                            user=admin
                            password=xxxx
                            dbname=rm',
                            %L) as x(%s)
    union all
        Select * from dblink(
                            'host=db02
                            user=admin
                            password=xxxx
                            dbname=rm',
                            %L) as x(%s)$R$,qr,alias,qr,alias);end;
$;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文