There are some limitations:
- Unable to modify database
- Columns are not unique.
- It is required to return the last insert identifier (
RETURNING id ) - If exists, return the existing id
- It will be called through our db user library (the values ββin select will be as parameters from PHP (?,?,?,!))
INSERT INTO tags (name, color, groupid) SELECT 'test', '000000', 56 WHERE NOT EXISTS ( SELECT text FROM tags WHERE name = 'test' AND groupid = 56 ) RETURNING id
This works - up to the point where I need to get an existing identifier as well. With this, I only get the inserted identifier. Is it possible to return the value of a SELECT statement if it is not inserted?
UPDATE:
DO $$ BEGIN IF NOT EXISTS ( SELECT text FROM tags WHERE name = 'test' AND groupid = 56 ) THEN INSERT INTO tags (name, color, groupid) VALUES ('test', '000000', 56) RETURNING id; ELSE RETURN SELECT text FROM tags WHERE name = 'test' AND groupid = 56; END IF; END $$
It was checked with this format - however, this ends with several errors:
RETURN cannot have a parameter in function returning void
sql php postgresql
Mauro tamm
source share