Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7616675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:56:47+00:00 2026-05-31T02:56:47+00:00

I am trying to create a simple stored procedure in PostgreSQL within which I

  • 0

I am trying to create a simple stored procedure in PostgreSQL within which I want to execute a SELECT statement. I do not want to return the results of the select statement.

If I use the select statement directly like the following code, I get an error saying that “query has no destination for result data”

DECLARE
affected_rows INTEGER;
BEGIN
SET search_path TO testing;

    SELECT * FROM testing.csv_data_already_imported;

    GET DIAGNOSTICS affected_rows := ROW_COUNT;
    RETURN affected_rows;
END;

Therefore I use the PREPARE .. EXECUTE statements like so:

DECLARE
affected_rows INTEGER;
BEGIN
SET search_path TO testing;

    PREPARE fooplan AS SELECT * FROM testing.csv_data_already_imported;
    EXECUTE fooplan;

    GET DIAGNOSTICS affected_rows := ROW_COUNT;
    RETURN affected_rows;
END;

Using this method still gives me errors:

ERROR:  column "fooplan" does not exist
LINE 1: SELECT fooplan
               ^

QUERY:  SELECT fooplan
CONTEXT:  PL/pgSQL function "returning_affected_rows" line 8 at EXECUTE statement

Can anyone help me understand what is wrong? The stored procedure is declared to be a plpgsql function meaning that the EXECUTE command should not be considered as being the standard SQL command to execute dynamic SQL statements. All I want to know is how to execute a SELECT statement inside a PostgreSQL stored procedure without needing to return its results. The full DDL of the stored procedure is as follows:

CREATE OR REPLACE FUNCTION testing.returning_affected_rows ()
RETURNS integer AS
$body$
DECLARE
    affected_rows INTEGER;
BEGIN
    SET search_path TO testing;

    PREPARE fooplan AS SELECT * FROM testing.csv_data_already_imported;
    EXECUTE fooplan;

    GET DIAGNOSTICS affected_rows := ROW_COUNT;
    RETURN affected_rows;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:56:49+00:00Added an answer on May 31, 2026 at 2:56 am

    You need PERFORM “to execute a SELECT statement inside a postgresql stored procedure without needing to return its results”.

    Otherwise your problem is that PL/pgSQL’s command EXECUTE is different than the SQL command EXECUTE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple stored procedure which I can use to write
I'm trying to create a simple stored procedure with an if else statement in
I'm trying to create a simple stored procedure which queries with Linked server. create
I'm trying to create a simple stored procedure which queries a sys.tables table. CREATE
I'm trying to execute a simple stored procedure with Spring/Hibernate using Annotations. Here are
I am trying to write a simple Oracle Stored Procedure: CREATE OR REPLACE PROCEDURE
I am trying to create a simple stored procedure in phpmyadmin. But the page
I am trying to create a simple stored procedure in SQL Server 2005, and
I'm trying to create a simple stored procedure to count the number of empty
I have a really simple stored procedure that looks like this: CREATE PROCEDURE _Visitor_GetVisitorIDByVisitorGUID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.