首页 > 解决方案 > 如果变量为空,则插入默认值

问题描述

我有一个t带有列的表c。该列c具有默认值。

create table t (c text default 'a');

我可以插入默认值。

insert into t (c) values (default);

现在我想将变量的值插入表中。如果变量为空,我想插入默认值。

do $$declare
  v text;
begin
  insert into t (c) values (coalesce (v, default));
end$$;

但它不起作用。

错误:在此上下文中不允许 DEFAULT

如何解决这个问题?我需要两个不同的插入语句吗?

标签: sqlpostgresqlplpgsql

解决方案


推荐阅读