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

  • Home
  • SEARCH
  • 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 7897863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:16:40+00:00 2026-06-03T08:16:40+00:00

i am writing a folloing pgsql procedure : CREATE OR REPLACE FUNCTION KNN(gid_ integer)

  • 0

i am writing a folloing pgsql procedure :

CREATE OR REPLACE FUNCTION KNN(gid_ integer)
RETURNS Text AS $body$
DECLARE
row_ RECORD;
BEGIN
    SELECT  g1.gid As SOURCE, g2.gid As Neighbors  FROM polygons as g1, polygons as   g2  WHERE g1.gid = $1 and g1.gid <> g2.gid  ORDER BY g1.gid, 
ST_Distance(g1.the_geom,g2.the_geom) limit 5;
END
$body$
LANGUAGE plpgsql;

Now that the query return 5 rows for each value of arrgument supplied to procedure. How can i return those 5 rows. Also, how can i execute the procedure for all values of argument stored in a table polygons as column gid. Please somebody give the full code please. thankful to you.

  • 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-06-03T08:16:42+00:00Added an answer on June 3, 2026 at 8:16 am

    You can use the RETURNS TABLE syntax to implicitly create OUT variables:

    CREATE OR REPLACE FUNCTION KNN(
        gid_ integer
    ) RETURNS TABLE (
        source  integer,
        neighbor integer
    ) LANGUAGE SQL AS $$
        SELECT g1.gid As SOURCE
             , g2.gid As Neighbors
          FROM polygons AS g1,
               polygons AS g2
         WHERE g1.gid = $1
           AND g1.gid <> g2.gid
         ORDER BY g1.gid
             , ST_Distance(g1.the_geom,g2.the_geom)
         LIMIT 5;
    $$;
    

    To use it, use SELECT * FROM KNN(42) and you will get back up to five two-column rows.

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

Sidebar

Related Questions

I'm writing the following function to format some text from a file object 'item':
I am just started writing the PL/pgSQL Trigger function. I am having couple of
I'm trying to learn Lisp (elisp, actually), and I tried writing the following function
In php I'm writing the following <?php if(x != 0) { echo function myfunction(){};;
I am writing following code, using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace
I am writing following statement to create a table in oracle database but failed
I'm looking for a way to create a dictionary without writing the key explicitly.
I am writing the following code in order to create a PDF file with
I am writing the following function the second time in unrelated projects (first was
I am writing the following recursive lambda function: #include <iostream> #include <functional> auto term

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.