D
D
des1roer2016-04-21 07:45:45
PostgreSQL
des1roer, 2016-04-21 07:45:45

Postgres insert coalesce?

this query is not working

INSERT INTO 
  geo
(
  num_order
)
VALUES (
  COALESCE(max(num_order) + 1, 1)
);

although select COALESCE(max(num_order) + 1, 1) works fine

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Shelemetiev, 2016-04-25
@des1roer

Use SELECT instead of VALUES:

INSERT INTO 
  geo
(
  num_order
)
SELECT  COALESCE(max(num_order) + 1, 1) from geo;

This code will work if there is at least one record in geo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question