Skip to content
Advertisement

Postgres stored procedure(function) confusion

I’m pretty new to Postgres and SQL as a whole and could use a bit of help with a function here.

I have this table:

What i need is for a function to take as input plate, start date and end date and throw an error if the table contains any row with the same plate where the rental period is different from but overlaps with the given period.

I found this document on reporting errors but I’m not sure how to properly format the function in order to implement what I need. https://www.postgresql.org/docs/9.6/plpgsql-errors-and-messages.html

Advertisement

Answer

The WHERE condition of a query you could use in a trigger could be:

Here $1, $2 and $3 are placeholders for the data against which you want to check, and the query becomes simple by using the “overlaps” range operator &&.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement