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 8729539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:51:07+00:00 2026-06-13T08:51:07+00:00

I don’t know how to write stored procedures in postgresql. I tried to write

  • 0

I don’t know how to write stored procedures in postgresql. I tried to write one and I’m getting the wrong output.

create or replace function getUserId() returns setof integer as 
$BODY$
Declare r integer;cnt integer;
BEGIN
for r in select distinct user_id from system_audit
loop
for cnt in select count(*) from system_audit where user_id=r
loop
return next cnt;
end loop;
return next r;
end loop;
return;
end;
$BODY$
language 'plpgsql';

When I check the output by typing:-

select * from getUserId()

I get this as the Output:-

58
173
920
106
76
191
14413
1
2
242
1320
-1
2
249
1167
192
2
233
70
98
229
193
45
237
49
103
45735
115
10
201
28
167
308
96
1211
110
7
172
7
13
1614
22
16
178
21
170
7364
99
308
225
13
235
7
199
14
88
23566
118
359
4
9
231
98
165
174
212
21
149
14
105
77
97
416
180
1345
23
59
152
297
239
37
197
133
144

The output is correct But the problem is this is coming as a single column which contains the output of two columns- user_id and count. I want to see my output as two columns, one for count and another for user_id. Please help me finding the solution.

Thanks in advance!

  • 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-13T08:51:08+00:00Added an answer on June 13, 2026 at 8:51 am

    You need to declare the function as returns table (...) if you want to get a real result set.

    Your approach is overly complicated and will be extremely slow with large tables. The whole result can be obtained with a single SQL statement, which does not require looping with cursors:

    select user_id, count(*)
    from system_audit
    group by user_id
    

    This can be put into a SQL function (note: not plpgsql):

    create or replace function getUserId() 
       returns table (user_id integer, user_count integer)
    as   
    $BODY$
        select user_id, count(*)
        from system_audit
        group by user_id
    $BODY$
    language sql;
    

    Note that the use of quoted language names is deprecated. You should use e.g. plpgsql instead of 'plpgsql' or sql instead of 'sql'.

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

Sidebar

Related Questions

Don't know what's wrong here, when I run the application it says Specified method
Don't know where else to ask, but from one day to the other my
Don't know if it's I'm doing it wrong or if there's a bug (I
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't know the term for the red validation border, does it have one? I
Don't know much about running a function on every item in an array, still
don't know if the title describes anything about what I'm trying to say but
don't know what happened in my jcrop selection. I think I pressed some keys
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't really know how to formulate the title, but it should be pretty obvious

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.