Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to return values for stored procedure in postgresql?

Asked by David Edmunds Mar 14, 2023 8.4K views 3 answers
Share

About this question

I was reading this on PostgreSQL Tutorials:

In case you want to return a value from a stored procedure, you can use output parameters. The final values of the output parameters will be returned to the caller.

And then I found a difference between function and stored procedure at DZone:

Stored procedures do not return a value, but stored functions return a single value

Can anyone please help me resolve this.

If we can return anything from stored procedures, please also let me know how to do that from a SELECT statement inside the body.

postgresqlstored-procedures

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Mar 17, 2025

If you’re working with stored procedures in PostgreSQL and need to return values, here’s how you can do it:

1. Understanding Stored Procedures vs. Functions

In PostgreSQL, a stored procedure (CALL procedure_name()) does not return values directly.

If you need to return values, you should either:

  •  Use OUT parameters in a procedure.
  •  Use a function (SELECT function_name()), which can return a value or table.

2. Using OUT Parameters in a Stored Procedure

If you need to return multiple values, use OUT parameters:

CREATE PROCEDURE get_user_details(IN user_id INT, OUT username TEXT, OUT email TEXT)
LANGUAGE plpgsql
AS $$
BEGIN
    SELECT name, email INTO username, email FROM users WHERE id = user_id;
END;
$$;

Calling the procedure:

  CALL get_user_details(1, NULL, NULL);

OUT parameters will hold the returned values.

3. Using a FUNCTION Instead (Preferred for Returning Values)

If you need a single return value or a table, use a function instead:

CREATE FUNCTION get_user_email(user_id INT) RETURNS TEXT AS $$
DECLARE email TEXT;
BEGIN
    SELECT users.email INTO email FROM users WHERE id = user_id;
    RETURN email;
END;
$$ LANGUAGE plpgsql;

Calling the function:

  SELECT get_user_email(1);

 Functions can be used in SELECT queries, unlike procedures.

4. Key Takeaways

  •  Stored procedures don’t return values directly—use OUT parameters.
  •   Use functions (RETURNS) if you need to return a value or table.
  •   Functions can be used in SELECT statements, making them more flexible.


Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 26, 2024

In PostgreSQL, you can return values from a stored procedure using the RETURN statement. Here's an example of how to define and return values from a stored procedure:


CREATE OR REPLACE FUNCTION get_employee_salary(employee_id INT)
RETURNS INT  -- Define the return type
AS $$
DECLARE
    salary INT;
BEGIN
    -- Retrieve the salary for the given employee_id
    SELECT emp_salary INTO salary
    FROM employees
    WHERE emp_id = employee_id;
    -- Return the salary
    RETURN salary;
END;
$$ LANGUAGE plpgsql;

In this example:

  • We create a stored procedure named get_employee_salary.
  • The procedure takes one parameter, employee_id, and returns an integer value (the employee's salary).
  • Within the procedure, we declare a local variable salary to store the retrieved salary value.
  • We use a SELECT INTO statement to fetch the salary for the given employee_id from the employees table and store it in the salary variable.
  • Finally, we use the RETURN statement to return the value of salary.

To call this stored procedure and retrieve the returned value, you can use a SQL query like this:



SELECT get_employee_salary(123);

This will execute the stored procedure and return the salary value for the specified employee. If you're executing the procedure from within another PL/pgSQL block or function, you can also capture the returned value using variables.

Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.