如何在更新语句中访问整个当前记录
How to access the whole current record within update statement
是否可以reference/access/pass更新语句中的当前记录?
CREATE TABLE t1 (
id serial PRIMARY KEY,
name text
);
CREATE TABLE t2 (
id serial PRIMARY KEY,
name text,
foo text
);
CREATE FUNCTION gen_t2_foo(_a t1, _b t2) RETURNS text AS $$
SELECT _a.name || ' - ' || _b.name;
$$ LANGUAGE sql;
CREATE FUNCTION upd_t2(_min_id int, _max_id int, _a t1) RETURNS VOID AS $$
UPDATE t2 SET
foo = gen_f2_name(_a, ???) -- How to pass the current t2 record?
WHERE id >= _min_id AND id <= _max_id;
$$ LANGUAGE sql;
看起来第二个函数应该从两个表中的字段 name
以及开始和结束索引之间创建一个值,对吧?在这种情况下,请确保 gen_t2_foo 也收到一个索引以合并单个记录(在两个表中)的值。然后,你想怎么用就怎么用。
顺便问一下,您为什么要将 upd_t2 声明为 returns 无效的函数?这就是 程序 的目的。
参考table:
create function upd_t2(_min_id int, _max_id int, _a t1)
returns void as $$
update t2
set foo = gen_t2_foo (_a, t2)
where id >= _min_id and id <= _max_id;
$$ language sql;
是否可以reference/access/pass更新语句中的当前记录?
CREATE TABLE t1 (
id serial PRIMARY KEY,
name text
);
CREATE TABLE t2 (
id serial PRIMARY KEY,
name text,
foo text
);
CREATE FUNCTION gen_t2_foo(_a t1, _b t2) RETURNS text AS $$
SELECT _a.name || ' - ' || _b.name;
$$ LANGUAGE sql;
CREATE FUNCTION upd_t2(_min_id int, _max_id int, _a t1) RETURNS VOID AS $$
UPDATE t2 SET
foo = gen_f2_name(_a, ???) -- How to pass the current t2 record?
WHERE id >= _min_id AND id <= _max_id;
$$ LANGUAGE sql;
看起来第二个函数应该从两个表中的字段 name
以及开始和结束索引之间创建一个值,对吧?在这种情况下,请确保 gen_t2_foo 也收到一个索引以合并单个记录(在两个表中)的值。然后,你想怎么用就怎么用。
顺便问一下,您为什么要将 upd_t2 声明为 returns 无效的函数?这就是 程序 的目的。
参考table:
create function upd_t2(_min_id int, _max_id int, _a t1)
returns void as $$
update t2
set foo = gen_t2_foo (_a, t2)
where id >= _min_id and id <= _max_id;
$$ language sql;