0

i want to display the stored procedure select query data as table in pgadmin I want to display the stored procedure select query output through the java spring boot app by using JDBC template. I can do this by using stored function in Postgres but not able to do it through stored procedure of postgres. here my procedure is created and called it is working but my table data is not displaying out in pgadmin

stored procedure CREATE PROCEDURE get_mobiles(brand_name varchar) LANGUAGE SQL
AS $$
SELECT * FROM mobile_phone where brand_name = brand_name; $$;

can we able to display the select query procedure output in postgres pgadmin without function, if yes means how? and how can feed these inputs to my JDBC template

1 Answer 1

0

With your current code there is no way to display the query output. You have a couple of issues.

  1. In order to return those results you would need to define an OUT parameter to hold those results. You have no OUT parameter.
  2. Your where clause is effective where 1=1. In SQL when a object name matches a column name and there is no further qualification that name always applies the the table column. Since your parameter name in the same as the column name it always matches. This is very very bad parameter naming; Always prefix or suffix the parameter name or qualify it with the procedure name (so here where brand_name=get_mobiles.brand_name).
  3. Finally while a procedure can be used this should be a function instead.

Further this can be done in a single SQL statement so need for plpgsql at all; just use language sql. So: (see demo here.)

create or replace function get_mobiles(brand_name_in varchar)
  returns mobile_phones
 language sql 
as $$
    select * 
      from mobile_phones 
     where brand_name = brand_name_in;
$$;

Notice the use of the table name as the data type. Actually it is not the table itself. When Postgres creates a table it also created a type of the same name, it is that type which is referenced.

NOTE: Demo includes how it is done with a Procedure. Unfortunately db<>fiddle does not show the raise notice ... messages. You will have to run the procedure/do on your own.

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.