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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:47:26+00:00 2026-06-18T02:47:26+00:00

I have a SP that returns quite a lot of data every second and

  • 0

I have a SP that returns quite a lot of data every second and is shown in a grid. I’m now trying to reduce bandwidth and thinking of returning only columns currently shown in my grid.

This is of course simplified and minimized but basically what I had was the following SP:

SELECT
[Animals].[AnimalID] AS [AnimalID],
[Animals].[name] AS [AnimalName],
[Foods].[DisplayName] AS [Food],
[Animals].[Age] AS [AnimalAge],
[Animals].[AmountOfFood] AS [AmountOfFood]

What I’m currently trying is to pass a TVP of the names of the fields (@fields) currently shown on the grid and returning only required fields as such:

SELECT
[Animals].[AnimalID] AS [AnimalID],
[Animals].[name] AS [AnimalName],
CASE 
    WHEN ('Food' in (select * from @fields)) 
    THEN [Foods].[DisplayName] 
END AS [Food],
CASE 
    WHEN ('AnimalAge' in (select * from @fields)) 
    THEN [Animals].[Age] 
END AS [AnimalAge],
CASE 
    WHEN ('AmountOfFood' in (select * from @fields)) 
    THEN [Animals].[AmountOfFood] 
END AS [AmountOfFood]

The problem I’m facing is that (as could be expected) my SP went from taking ~200 ms to taking ~1 sec

Is there any way to maybe rewrite this so that it doesn’t kill us?

My kingdom for a foreach!!!

  • 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-18T02:47:27+00:00Added an answer on June 18, 2026 at 2:47 am

    In SQL Server, you can also do this with dynamic SQL. Something like:

    declare @sql nvarchar(max);
    
    select @sql = (select ', '+
                          (case when FieldName = 'Food' then 'Foods.DisplayName'
                                when FieldName = 'AnimalAge' then 'Animals.Age'
                             . . .
                           end)
                   from @fields
                   for xml path ('')
                  );
    
    select @sql = 'select [Animals].[AnimalID] AS [AnimalID], [Animals].[name] AS [AnimalName]'+@sql+RESTOFQUERY;
    
    exec(@sql);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view which returns quite a lot of data. Right now, the
I have a piece of data that takes quite a lot of time to
I have a lot of legacy code that is now a backend for a
I have a query that uses a lot of aggregate functions to build data
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returns module path of given class name def findModulePath(path, className):
I have a function that returns two values in a list. Both values need
I have a javascript that returns a value whenever I choose an option from
I have a function that returns the request parameters for each request: private function
I have a function that returns log10 values. On converting them to normal numbers,

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.