1
We have the following simplified table for the example:
create table teste(
id serial not null
);
The commands below cause syntax error:
insert into teste(
select (case when 1=1 then DEFAULT end)
);
or:
insert into teste(
select coalesce(null::integer, DEFAULT)
);
ERROR: syntax error at or near "DEFAULT" LINE 2: select (case when 1=1 then DEFAULT end) ^ ********** Error ********** ERROR: syntax error at or near "DEFAULT" SQL state: 42601
Provisionally use the following:
insert into teste(
select coalesce(null::integer, nextval('teste_id_seq'::regclass))
);
The objective is to inform the DEFAULT value of the field, whether serial or not, conditionally without informing the function or the value of the constant defined in the table, because this value can change in the future and will cause an inconsistency in the commands that call the INSERT. That’s possible?
The word DEFAULT should be entered in the INSERT VALUES part and not in the field list.
– anonimo
@anonimo Thank you, but note that the above example DEFAULT is not in the field declaration, INSERT is done only with the value, which is one of the ways to do this without necessarily giving the field names, since values that are in the same order of types accepted by the record type of the table.
– AnselmoMS
I don’t understand. It makes no sense that you want to specify the term DEFAULT if in the creation of the table you have not informed what is the DEFAULT value.
– anonimo
@anonymity:"...when creating the table you did not specify what is the DEFAULT value". Implicitly define yes. Note that the table statement looks like this:
...id integer **default** nextval('teste_id_seq'::')
– AnselmoMS