sql - 如果变量为空,则插入默认值
问题描述
我有一个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
如何解决这个问题?我需要两个不同的插入语句吗?
解决方案
推荐阅读
- java - 从不同类的数组中获取元素
- javascript - 当我使用它来导入axios API时如何解决require的引用错误?
- java - ElementNSImpl 类型的对象以 com.sun.org.apache.xerces.internal.dom.ElementNSImpl 的形式出现
- opencv - 具有各种“深度”的场景的平均高档多图像超分辨率的巧妙解决方案
- sql - 必须声明标量变量“@LocationId”
- sql - 如何更新数据库函数的列值?
- java - java.lang.ClassNotFoundException: services.Login
- ibm-cloud - 什么是 Cloudant 文档过期字段?
- javascript - 试图将 url 段发布到 firebase
- javascript - 日期格式短信