使用这个:
update mytable set a =
coalesce(incidentid,
(
select column_default::int
from information_schema.columns
where table_schema = 'public'
and table_name = 'mytable' and column_name = 'incidentid')
)
如果您的incidentid是整数类型,请在默认列上放置类型转换
[编辑]
create or replace function get_default_value(_table_name text,_column_name text)
returns text
as
$$
declare r record;
s text;
begin
s = 'SELECT ' || coalesce(
(select column_default
from information_schema.columns
where table_schema = 'public'
and table_name = _table_name and column_name = _column_name)
, 'NULL') || ' as v';
EXECUTE s into r;
return r.v;
end;
$$
language 'plpgsql';
使用:
update mytable set a =
coalesce(incidentid, get_default_value('mytable','a')::int )