vendredi 15 janvier 2021

How to check if an sequence is above a certain number and if not change it in Postgres

I have a problem where my SQL sequence has to be above a certain number to fix a unique constraint error. Now I started to write an if-statement that checks for a certain number and if it's below it should be increased to a certain number. The statement is for Postgres.

I got the separate parts running but the connection over if is throwing an error and I don't know why.

First for selecting the current number:

SELECT nextval('mySequence')

Then to update the number:

SELECT setval('mySequence', targetNumber, true)

The full statement looks something like this in my tries:

IF (SELECT nextval('mySequence') < targetNumber)
THEN (SELECT setval('mySequence', targetNumber, true))
END IF;

and the error is

ERROR:  syntax error at »IF«

Can someone explain to me what I did wrong there because the error message isn't giving me much to work with? I would appreciate your help.

Aucun commentaire:

Enregistrer un commentaire