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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:56:03+00:00 2026-05-11T21:56:03+00:00

I have some stored procedures that I need to write against a nasty beast

  • 0

I have some stored procedures that I need to write against a nasty beast of an database. I need to loop through a table (application) and pull values out of other tables (some are aggerate / averages /etc values) using the application_id from the application table.

So far I have:

declare @id INT
declare app cursor for
    SELECT application_id from application
OPEN app
FETCH NEXT FROM app 
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT a.NAME_LAST, a.NAME_FIRST, ca.status, (SELECT AVG(score) FROM reviews WHERE application_id = @id), (SELECT count(*) FROM reviews WHERE application_id = @id)  FROM application a, committee_applications ca WHERE a.application_id = ca.application_id AND a.application_id = @id
  FETCH NEXT FROM app INTO @id
END
CLOSE app
DEALLOCATE app

Which is giving me the results I want, but I’m sure there is a cleaner way of doing this, and I can’t seem to make the mental jump today to do this correctly. Could someone point out a better way of doing this as this seems really ugly to me.

Also, it seems like I should be storing these values into a temp table then returning the full results instead of running the SELECT statement one by one.

Any suggestions would be greatly appreciated.

Thanks.

  • 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-11T21:56:04+00:00Added an answer on May 11, 2026 at 9:56 pm

    Removing the cursor will probably speed this up a lot. By using a derived table, you can get the counts and averages with one query and join that back to the other tables to get the remaining columns. Like this….

    SELECT a.NAME_LAST, 
           a.NAME_FIRST, 
           ca.status, 
           Scores.AverageScore,
           Scores.CountScore
    FROM   application a
           Inner Join committee_applications ca 
             On a.application_id = ca.application_id
           Left Join (
             SELECT application_id, 
                    AVG(score) As AverageScore, 
                    Count(*) As CountScore
             FROM   reviews 
             Group By application_id
             ) As Scores
             On a.application_id = Scores.application_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have about 140 - 150 stored procedures that I need to migrate from
I have some stored procedures to execute that use dbms_output.put_line() to put data into
I have a stored procedure that logs some data, how can I call this
I have a stored procedure that is returning a few fields, most containing some
I have a stored procedure that does, among other stuff, some inserts in different
I have been messing around with writing some stored procedures in .NET code with
I am attempting to tune some stored procedures and have a question on indexes.
I have some enterprise apps running on Java 1.4. They mostly invoke Stored Procedures
We have some views and stored procedures which have some data hard-coded in their
I'm new into SQL and stored procedures, and need some help with the stored

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.