1

I have a function and I need to do different select based on inputid. so I have added the condition as below.

....
begin
IF(iv_binrocess = 1 AND IsNull(iv_binno,'') != '') THEN
...//some process here 
END If;
...
End

When I run the function, I got the error "

ERROR: syntax error at or near "IF" I referred many sites and tutorials, nothing helped me.

Can anyone help me to fix this issue?

Thanks

3
  • 4
    I suspect that you have stripped enough code from the sample you posted to strip the problem with it. Please post the complete code surrounding the error. Are you sure that you are looking at the correct IF in the function body? Commented Jul 26, 2017 at 11:14
  • Show us your complete code. Edit your question - do not post code or additional information in comments. Commented Jul 26, 2017 at 11:36
  • Also, you added a tag of Postgres, but Postgres does not have IsNull function, its equivalent in Postgres is coalescesee documentation. Commented Jul 26, 2017 at 12:32

1 Answer 1

1

WAG, since you deleted most of the important information: You're trying to create a function with a PL/pgsql body, but declaring it SQL.

craig=> CREATE FUNCTION test() RETURNS void LANGUAGE sql AS 
$$ BEGIN IF TRUE THEN PERFORM 1; END IF; END; $$ ;
ERROR:  syntax error at or near "IF"
LINE 1: ...TION test() RETURNS void LANGUAGE sql AS $$ BEGIN IF TRUE TH...

Declare PL/PgSQL functions as LANGUAGE plpgsql.

If you instead want to use an SQL function, use a CASE expression instead of IF.

CREATE FUNCTION test2() RETURNS integer LANGUAGE sql 
AS $$ SELECT CASE WHEN TRUE THEN 1 ELSE 0 END; $$;

(Note, I haven't bothered to format these readably. Don't write functions all in one line like this in real code.)

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.