sql - 在 RETURNING 语句中使用记录作为函数参数

标签 sql postgresql sql-insert plpgsql sql-function

我有一个将记录作为参数的 plpgsql 函数。

我想在 RETURNING 中调用这个函数插入语句,但不确定将什么作为参数(* 不起作用)即,

-- Postgres 12

INSERT INTO some_table (a, b, c)
VALUES .....
RETURNING
function_that_takes_record_argument(<the_new_record>)

我可以用什么代替 <the_new_record>

最佳答案

要将记录/行传递给函数,您需要明确指定记录类型:

create table t(a int, b text);

create or replace function f(r record) returns int language plpgsql as $$
begin
    return r.a;
end $$;

insert into t values(2, 'b');

select f(t.*), f(t), f((a,b)::t) from t;

 f | f | f 
---+---+---
 2 | 2 | 2

insert into t values(3, 'c') returning f(t);

 f 
---
 3

关于sql - 在 RETURNING 语句中使用记录作为函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61109764/

相关文章:

sql - Azure 中具有聚集索引的表出现聚集索引错误

sql - Postgres 一贯偏爱嵌套循环连接而不是合并连接

database - 提高 PostgreSQL 中的 OFFSET 性能

mysql - 将多行很长的字符串插入 MySQL 数据库

sql - 选择具有不同列名的表

sql - 无法加入备忘录、OLE 或超链接对象 - Access 2007 - 外部联接

java - 我无法从 plpgsql 函数中读取 java 返回的数组

sql - 如何一次将所有表列设置为 NOT NULL?

sql - 如何插入包含撇号(单引号)的值?

mysql - 如果两列具有特定值,如何设置值?