2
CREATE OR REPLACE FUNCTION  udf_get_emp_name(p_empcode integer)
returns text
AS 
$BODY$

    DECLARE l_emp_name  TEXT;
   select emp_name into l_emp_name from employee  where empcode = p_empcode;

   return l_emp_name;
END;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

This function gets created successfully and postgresql doesn't check if table exist or column exist. Is there any option to check if column name and table names are correct and exist at the time of CREATE or REPLACE FUNCTION

1 Answer 1

4

You don't need PL/pgSQL for this. If you make that a plain sql function, Postgres will check the presence of the table and the syntax of the embedded SQL statements:

CREATE OR REPLACE FUNCTION  udf_get_emp_name(p_empcode integer)
returns text
AS 
$BODY$
   select emp_name 
   from employee 
   where empcode = p_empcode;
$BODY$
  LANGUAGE sql VOLATILE
  COST 100;

If however you simplified your example and you do really need PL/pgSQL, then you can use the extension PL/pgSQL Check (or PL/pgSQL Lint) to verify the SQL code in PL/pgSQL functions.

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

2 Comments

Thanks for the reply. But if i use sql, then i am unable to return query option .
@SayeekrishnanSubramaniam: A SQL function can return the result of a query (as shown in the example in my answer)

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.