Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function postgresql-更新-函数的可见性_Function_Postgresql_Visibility - Fatal编程技术网

Function postgresql-更新-函数的可见性

Function postgresql-更新-函数的可见性,function,postgresql,visibility,Function,Postgresql,Visibility,假设我有一个函数副本(in integer,out integer),它在表mytable中创建由in参数标识的行的副本。将返回新行的标识符 我想对新行/重复行应用更新,而不是旧行: update mytable set field = ... where identifier = (select copy(1)); 这似乎不起作用。已创建副本,但仍具有旧值。我希望在计算where子句时,新行还不可见。也就是说,没有更新发生 以下操作也不起作用: update mytable set fiel

假设我有一个函数副本(in integer,out integer),它在表mytable中创建由in参数标识的行的副本。将返回新行的标识符

我想对新行/重复行应用更新,而不是旧行:

update mytable set field = ... where identifier = (select copy(1));
这似乎不起作用。已创建副本,但仍具有旧值。我希望在计算where子句时,新行还不可见。也就是说,没有更新发生

以下操作也不起作用:

update mytable set field = ... from copy(1) as c where identifier = c.copy;
当我把它写成两行时,它就完美地工作了:

select copy(1);
update mytable set field = ... where identifier = <value returned by copy(1)>;
在后台创建副本并在复制/新行上应用更新时?我看不出一个解决方案不会以无休止的循环结束

示例代码

drop table if exists t cascade;

create table t
(
    identifier  serial  primary key,
    title       text
);

create or replace function copy(in integer, out integer) as
$$
        begin
                insert into t (title) values ((select title from t where identifier = $1)) returning identifier into $2;
        end
$$ language plpgsql;

insert into t (title) values ('title - old');
update t set title = 'title - new' where identifier = (select copy(1));
select * from t;

请注意,在copy()中,行通常被提取到单独的变量中。为了简单起见,我在这个示例代码中直接获取了标题。

我认为它不起作用的原因与除了第9.1页以后的语句外,不能使用返回语句的原因相同。这在9.1中应该可以正常工作:

drop table if exists t cascade;

create table t (
    identifier  serial  primary key,
    title       text
);

insert into t (title) values ('title - old');

with copy as (
insert into t (title) select title from t where identifier = 1
returning identifier
)
update t set title = 'title - new' from copy where t.identifier = copy.identifier;

select * from t;


编辑:可能的9.0建议不起作用。

我认为它不起作用的原因与您不能使用Return in with语句的原因相同,除非从第9.1页开始。这在9.1中应该可以正常工作:

drop table if exists t cascade;

create table t (
    identifier  serial  primary key,
    title       text
);

insert into t (title) values ('title - old');

with copy as (
insert into t (title) select title from t where identifier = 1
returning identifier
)
update t set title = 'title - new' from copy where t.identifier = copy.identifier;

select * from t;


编辑:可能的9.0建议不起作用。

你能发布
copy()
函数代码吗?Kev,我添加了一些示例代码来说明这一点。请注意,这是快速和肮脏的,但它解释了我的问题,我希望。你能发布
copy()
函数代码吗?Kev,我添加了一些示例代码来说明这一点。请注意,这是快速和肮脏的,但它解释了我的问题,我希望。谢谢你的检查(我不能,在iPad上)。我在9.1 beta 3中尝试了你的建议,它仍然返回一个新的行,但返回了旧的标题。(我不得不在update语句中添加“from copy”)。有什么想法吗?@Potige:我已经修改了它,添加了from子句,并对它进行了测试——它在9.1 beta3.1中运行良好。谢谢你的检查(我不能,在iPad上)。我在9.1 beta 3中尝试了你的建议,它仍然返回一个新的行,但返回了旧的标题。(我不得不在update语句中添加“from copy”)。有什么想法吗?@Potige:我已经修改了它,添加了from子句,并对它进行了测试——它在9.1 beta3中运行良好。