how do you use a value that came out of the select statement in an insert?

You can do this in a single insert ... select statement:

insert into form (versionnumber, name, state, timestamp)
select coalesce(max(versionnumber), 0) + 1, $1, 1, now()
from form
where name = $1;

The logic is to search for the last version number of the form in the table (with a default of 0), and increment it.

$1 is the parameter to the query, that should be given the form name. I doubt that you need a trigger to do what you want.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top