0

I create a postgres function like this:

CREATE OR REPLACE FUNCTION delete_annotation_f(
    IN cmtid uuid)
    RETURNS bool AS $$
DECLARE
     pid uuid;
     cmt_cnt int4;
BEGIN
    SELECT get_comment_cnt_f(cmtid) INTO cmt_cnt;
    UPDATE detail_t SET ann_cmt_cnt=ann_cmt_cnt - cmt_cnt;
    RETURN TRUE;
END
$$
LANGUAGE plpgsql;

But when I run this function I get this error:

ERROR:  column reference "cmt_cnt" is ambiguous
LINE 1: ...detail_t SET ann_cmt_cnt=ann_cmt_cnt-cmt_cnt WH...

I find this link On Inset: column reference "score" is ambiguous but it could not help me solve the problem. Anyone have solutions?

1
  • 1
    table detail_t must have column cmt_cnt ?.. Commented Jan 24, 2018 at 13:34

2 Answers 2

1

You are using a variable that have the same name of a column. The query parser could not decide which it must chose, change your variable name then the ambiguity will vanish.

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

Comments

1

https://www.postgresql.org/docs/current/static/plpgsql-implementation.html

By default, PL/pgSQL will report an error if a name in a SQL statement could refer to either a variable or a table column. You can fix such a problem by renaming the variable or column, or by qualifying the ambiguous reference, or by telling PL/pgSQL which interpretation to prefer. The simplest solution is to rename the variable or column. A common coding rule is to use a different naming convention for PL/pgSQL variables than you use for column names. For example, if you consistently name function variables v_something while none of your column names start with v_, no conflicts will occur.

and further:

You can also set the behavior on a function-by-function basis, by inserting one of these special commands at the start of the function text:

#variable_conflict error
#variable_conflict use_variable
#variable_conflict use_column

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.