0

I'm writing some triggers to check if an arrtibute of a newly create tuple is present in another table. What is the best way to check this?

IF (SELECT * FROM TABLEB where NEW.Attribute = Attribute) = NULL THEN
return NULL
END IF

Is there a better way? And does a Select that returns nothing = NULL or empty set?

1 Answer 1

1

Use IF EXISTS (SELECT * FROM TABLEB where NEW.Attribute = Attribute).

An empty rowset isn't the same as NULL, which represents an empty cell of a row. Your syntax would only be valid if the query only selected one row, in which case PostgreSQL will allow use of comparison operators against the top cell returned, which counts as NULL if none are returned.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.