0

I have a trigger setup to fire after a row is updated or inserted for table A. There is also a table B that references table A, but also has another column which I want to analyze, a boolean (so this function would only be usable on an UPDATE).

When I try to access the column:

SELECT col1 FROM B WHERE B.aID = NEW.ID;

This column col1 is always NULL.

Why is this? How can I get the boolean value upon an update?

1 Answer 1

3

Most probably you are running into a naming conflict. Parameter names (IN and OUT parameters) are visible in the function body (almost) anywhere and take precedence over unqualified column names. Did you declare col1 as variable in the function?

To avoid the conflict, table-qualify the column name:

SELECT b.col1 FROM tableb b WHERE b.aID = NEW.ID;

This is good practice in any case.

It is also good practice to prefix variable names, so they wouldn't normally conflict with table columns. Like: _col1.

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

1 Comment

Bingo - my declared variable was indeed the same name as the column itself. Adding an underscore before the name (_col1) for the local function variable solved it! Thanks!

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.