调用postgresql函数时如何将多个变量传递给同一个参数

发布于 2025-01-11 04:37:47 字数 1023 浏览 0 评论 0原文

我的表中共有 4 条记录,

 id |                url                 |        name         | description | last_update
----+------------------------------------+---------------------+-------------+-------------
  1 | https://www.postgresqltutorial.com | PostgreSQL Tutorial |             |
  2 | http://www.oreilly.com             | O'Reilly Media      |             |
  3 | https://www.google.com             | Google              |             | 2013-06-01
  4 | http://www.postgresql.org          | PostgreSQL          |             |

我编写了一个通过将名称作为参数传递来删除的函数,现在我想传递多个名称,但我遇到了错误。

CREATE OR REPLACE FUNCTION testing(first_name varchar(255))

RETURNS INTEGER AS

$BODY$

DECLARE emp_id INTEGER;

BEGIN
 SELECT id into emp_id from links e where name = first_name;
 
 DELETE FROM links WHERE id = emp_id;

 return emp_id;

END

$BODY$

LANGUAGE plpgsql;

从测试中选择 *('Google, PostgreSQL');

错误:- 没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换

i have total of 4 records in my table

 id |                url                 |        name         | description | last_update
----+------------------------------------+---------------------+-------------+-------------
  1 | https://www.postgresqltutorial.com | PostgreSQL Tutorial |             |
  2 | http://www.oreilly.com             | O'Reilly Media      |             |
  3 | https://www.google.com             | Google              |             | 2013-06-01
  4 | http://www.postgresql.org          | PostgreSQL          |             |

i have written a function to delete by passing name as a parameter, now i want to pass multiple names but i am facing error.

CREATE OR REPLACE FUNCTION testing(first_name varchar(255))

RETURNS INTEGER AS

$BODY$

DECLARE emp_id INTEGER;

BEGIN
 SELECT id into emp_id from links e where name = first_name;
 
 DELETE FROM links WHERE id = emp_id;

 return emp_id;

END

$BODY$

LANGUAGE plpgsql;

select * from testing('Google, PostgreSQL');

Error:- no function matches the given name and argument types. you might need to add explicit type casts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

下壹個目標 2025-01-18 04:37:47

由于您有一个逗号分隔的列表,因此您可以使用 string_to_array 函数,然后应用 any 操作员。此外,没有理由使用 pgplsql,这可以写在单个 sql 语句中,然后将其包装到 sql 参数/函数中。 (请参阅演示

create or replace procedure testing(first_names varchar(255))
language sql 
as $
   delete from links 
    where name = any(string_to_array(first_names, ',')); 
$;

Since you have a comma separated list, you can cast your parameter as an array with string_to_array function then apply the any operator. Further there is no reason for pgplsql, this can be written in a single sql statement, then wrapped it into a sql parameter/function. (see demo)

create or replace procedure testing(first_names varchar(255))
language sql 
as $
   delete from links 
    where name = any(string_to_array(first_names, ',')); 
$;
吹泡泡o 2025-01-18 04:37:47

如果要传递多个值,则必须定义函数来接受多个值。有两种方法:

  1. 可变参数函数:

    CREATE FUNCTION 测试(VARIADIC first_name text[]) ...
    

    这就是所谓的

    SELECT 测试('arg1', 'arg2', 'arg3');
    
  2. 接受数组作为参数的函数:

    创建函数测试(first_name text[]) ...
    

    这就是所谓的

    SELECT 测试(ARRAY['arg1', 'arg2', 'arg3']);
    

If you want to pass several values, you have to define the function to accept several values. There are two ways:

  1. a variadic function:

    CREATE FUNCTION testing(VARIADIC first_name text[]) ...
    

    This is called like

    SELECT testing('arg1', 'arg2', 'arg3');
    
  2. a function that accepts an array as parameter:

    CREATE FUNCTION testing(first_name text[]) ...
    

    This is called like

    SELECT testing(ARRAY['arg1', 'arg2', 'arg3']);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文