- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定这张表:
create table test (
name text primary key
);
我需要编写一个变量名与主键名冲突的 plpgsql 函数,我必须在 on conflict
子句中使用它:
create or replace function func(
name text -- this variable name...
) returns void language plpgsql as
$$
begin
insert into test (name) values (name)
on conflict (name) do update -- ...conflicts with this line
set name = func.name;
end;
$$;
这会编译,但会抛出一个不明确的列引用:
select * from func('one');
ERROR: column reference "name" is ambiguous
LINE 2: on conflict (name) do update
^
DETAIL: It could refer to either a PL/pgSQL variable or a table column.
QUERY: insert into test (name) values (name)
on conflict (name) do update
set name = func.name
CONTEXT: PL/pgSQL function func(text) line 3 at SQL statement
我尝试将完整的列名称指定为 on conflict (test.name)
不编译,或 ((test.name))
编译:
create or replace function func(
name text
) returns void language plpgsql as
$$
begin
insert into test (name) values (name)
on conflict ((test.name)) do -- this fails too
update set name = func.name;
end;
$$;
但它也失败了:
select * from func('two');
ERROR: invalid reference to FROM-clause entry for table "test"
LINE 2: on conflict ((test.name)) do
^
HINT: There is an entry for table "test", but it cannot be referenced from this part of the query.
QUERY: insert into test (name) values (name)
on conflict ((test.name)) do
update set name = func.name
CONTEXT: PL/pgSQL function func(text) line 3 at SQL statement
有解决办法吗?
编辑:我找到了一个解决方法:
on conflict on constraint test_pkey do update
其中 test_pkey
是表名加上 _pkey
。我不知道这有多可靠。我仍然想改为指定列名。
最佳答案
首先,name
对变量和属性来说都是一个坏名字。当你同时拥有两者时,代码将不会好看。考虑到这一点,您可以使用带标签的 block “前缀”变量(在下面的示例中 <<fn>>``), and set
variable_conflict` 以优先考虑列名,请参见下面的代码:
t=# create or replace function func(
name text
) returns void language plpgsql as
$$
#variable_conflict use_column
<<fn>>
declare name text :='blah';
begin
insert into test (name) values (name)
on conflict (name) do -- this no longer fails
update set name = fn.name;
end;
$$;
t=# insert into test select 'b';
INSERT 0 1
Time: 8.076 ms
t=# select func('b');
func
------
(1 row)
Time: 6.117 ms
t=# select * from test;
name
------
b
blah
(2 rows)
https://www.postgresql.org/docs/current/static/plpgsql-implementation.html#PLPGSQL-VAR-SUBST
By default, PL/pgSQL will report an error if a name in a SQL statement could refer to either a variable or a table column. You can fix such a problem by renaming the variable or column, or by qualifying the ambiguous reference, or by telling PL/pgSQL which interpretation to prefer.
而且 - 基本上整个链接都是关于它的。
然而 - 在演示了如何使用 plpgsql 轻松完成特定任务之后,我仍然引用 namual:
The simplest solution is to rename the variable or column. A common coding rule is to use a different naming convention for PL/pgSQL variables than you use for column names. For example, if you consistently name function variables v_something while none of your column names start with v_, no conflicts will occur.
关于sql - 如何在 ON CONFLICT 子句中消除 plpgsql 变量名的歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47678934/
我是一名优秀的程序员,十分优秀!