0

I have created a stored procedure using PGADMIN4. Inside the SP, I have selected a view table. However, I want the data inside the stored procedure to be inserted into a new table. I have tried below code, but it is showing an error:

SP name: Test

New table name: Customer

Insert into public.Customer exec public.Test

This is my SP code:

create procedure test()
language plpgsql
as $$
BEGIN
Select * from public.customer_list; 

END; 
$$;

ERROR: syntax error at or near "exec"

4
  • 1) You will need to add the code of your stored procedure to your question 2) If you are actually using a SP it does not have RETURN , see here SP return 3) A SP needs to be used with CALL Commented Oct 16, 2021 at 17:34
  • Convert your procedure to a function that returns a table. Procedures aren't meant to return result sets Commented Oct 16, 2021 at 17:35
  • @AdrianKlaver i have added my sp code Commented Oct 16, 2021 at 17:41
  • You will need a function not a procedure. For this case the simplest way is RETURN QUERY, see here Returning 43.6.1.2. RETURN NEXT and RETURN QUERY and look at examples. Then something like Insert into public.Customer select * from public.test();. Commented Oct 16, 2021 at 17:45

1 Answer 1

1

Procedures cannot be used in SQL. If you must have a stored procedure (define as code stored in the database to be executed) then change to a SQL function. Which returns a type.

create function copy_customer_list()
  returns setof customer_list 
  language sql
as $$
   Select * from customer_list; 
$$;  

Then you can insert into other table with

insert into customer  
  select * from copy_customer_list();
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.