我正在测试一些条件渲染,以便在较小的范围内检查其是否按预期工作,然后再将其应用于更大的表格。
目标是创建一个更新查询,仅在用户输入了新数据的列上进行更新,如果没有数据,则不对这些列执行任何操作。
这是我的 case when then 测试。具有 null 值的行未更新 newValue
:
drop table if exists tester
create table tester (
id serial primary key
val TEXT,
)
insert into tester (val) values (null)
select * from tester
update tester
set val = case
when val = null
then 'newValue'
else val
end;
这是一个我想要生成的简单示例($1
是用户输入):
drop table if exists tester
create table tester (
id serial primary key
val TEXT,
)
insert into tester (val) values (null)
select * from tester
update tester
set val = case
when val != $1
then $1
else val end
where id = 1
我期望的结果是插入一行,该行的id为1,val单元格的值为null。在更新查询中,如果行1上存储的val数据与输入值不相等,则更新将检查这一点。如果成立,则会将输入值设置为行1的val值。这个逻辑和语法正确吗?如果不正确,请指出错误所在。
CASE WHEN val IS NULL
。使用等号是行不通的。 - Tim Biegeleisen$1
是什么,但是很可能会起作用。你正在使用带有Postgres的编程语言吗? - Tim Biegeleisen