调用postgresql函数时如何将多个变量传递给同一个参数
我的表中共有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您有一个逗号分隔的列表,因此您可以使用 string_to_array 函数,然后应用 any 操作员。此外,没有理由使用 pgplsql,这可以写在单个 sql 语句中,然后将其包装到 sql 参数/函数中。 (请参阅演示)
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)
如果要传递多个值,则必须定义函数来接受多个值。有两种方法:
可变参数函数:
这就是所谓的
接受数组作为参数的函数:
这就是所谓的
If you want to pass several values, you have to define the function to accept several values. There are two ways:
a variadic function:
This is called like
a function that accepts an array as parameter:
This is called like