SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Selecting columns based on user input?


Selecting columns based on user input?

Author
Message
gage.trader
gage.trader
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 11
I am not sure how to describe this and I'm sorry if the subject isn't very clear.

Here is what I want to do: I have to generate reports for my employer and sometimes he wants to see things at varying degrees of detail. For example:

(The example will have a Table called "Cars" with columns "Yr", "Make", "Model", and "Price")

Sometimes I need to know, say...the average price of all cars for a year (this query):


SELECT Yr, AVG(Price)
FROM Cars
GROUP BY Yr



And Sometimes I need the average price for all cars of a year and make (this query):


SELECT Yr, Make, AVG(Price)
FROM Cars
GROUP BY Yr, Make



Is there any way using just SQL to write a query that could handle both cases? I am integrating this within a .NET application so of course I could always build up the query string in my C# file depending on the requirements, but that feels so messy and is a real pain to debug, especially when you start adding rollups and whatnot. Also it seems that doing as much in SQL and as little in .NET is always best for performance.

I've thought of is to write a procedure for each "level" of detail, but the amount of redundancy within those procedures would be really high, and the last thing I want to do is update 4 procedures when I need to make a change.

I've also thought of writing a query that returns all of the data at every level and then just pick what is needed out of that, but that takes too much time.

If you have any suggestions, throw them my way! Thanks in advance for any help,
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89669 Visits: 14997
Your options are either to limit the selections the manager has and run a specific stored procedure for each option, return the all the detail data and do all the manipulation and roll up in your application, or do a single stored procedure that uses dynamic sql that returns the data as desired. The downside to the third option is that you need to be sure to protect against SQL Injection. Here are a couple of links to articles acknowledged to be some of the best about dynamic sql.

http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/dyn-search-2005.html



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Steve Jones
Steve Jones
SSC Guru
SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)

Group: Administrators
Points: 285337 Visits: 19917
The thing I'd suggest is that you write two stored procedures, which is essentially two methods. Then if you want to build an abstraction, write a 3rd that takes both parameters, if only one comes in (check for NULL), call one proc, otherwise call the other.

That seems like work, but if you build it up in your app, you're essentially doing the same thing. By encapsulating this into multiple procs, you can easily call them from other places, and also it is tune-able if more results or columns are needed.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
gage.trader
gage.trader
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 11
Aha, dynamic SQL is the term I needed.

I think I'm pretty well protected from SQL Injection by using .Net's SqlParameter object and specifying the type. I've seen in other projects where it has prevented some potentially malicious injection attacks.

I'll get to reading...thanks a bunch!
gage.trader
gage.trader
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 11
Steve Jones - Editor (7/20/2009)
The thing I'd suggest is that you write two stored procedures, which is essentially two methods. Then if you want to build an abstraction, write a 3rd that takes both parameters, if only one comes in (check for NULL), call one proc, otherwise call the other.

That seems like work, but if you build it up in your app, you're essentially doing the same thing. By encapsulating this into multiple procs, you can easily call them from other places, and also it is tune-able if more results or columns are needed.


That seems like a good solution, but it would ultimately lead to a lot of duplicating a lot of the same code. But, maybe if I put all of the complex joins and temporary tables into a view and then use the various procedures to aggregate the data from the view I could avoid having to rewrite all the crap. That might be just what I need.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)

Group: Administrators
Points: 285337 Visits: 19917
You're not necessarily duplicating lots of code. You might dup some, but you're also separating out and abstracting the functions to do a specific thing. That's what's done in many methods in OOP programming. You separate out those different items that perform different functions.

Don't forget, you copy this code and alter it a couple times. It's very little of your time. Trying to munge things together and having the end-user suffer later will happen over, and over again.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89669 Visits: 14997
I understood you to mean that you could have several different ways the manager wants to look at the data. If it is only those 2 then I would do as Steve has suggested.

To be honest, I have always preferred to write static reports for specific purposes. This way you can more easily optimize the T-SQL.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
gage.trader
gage.trader
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 11
Jack Corbett (7/20/2009)
I understood you to mean that you could have several different ways the manager wants to look at the data. If it is only those 2 then I would do as Steve has suggested.

To be honest, I have always preferred to write static reports for specific purposes. This way you can more easily optimize the T-SQL.


It's possible...One minute he wants one thing and then 2 years later he could decide he wants something completely different :-P

In general though, the data can really be seen on 4 levels: customer, item category, item subcategory, and item. So there are potentially 4 of them in the specific case I'm working on now. I'd kind of like to set up a best practice sort of thing though, for all of my future reports because that's really a lot of my job and it's not something that's ever been done very well here.

I definitely see your point about optimization though. At least you've both given me a good bit to chew on :-D
Steve Jones
Steve Jones
SSC Guru
SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)

Group: Administrators
Points: 285337 Visits: 19917
I'd be curious to know what you decide to do and why. It's a good debate, and others can learn from this. If you have a blog, post it there and link from here. Or write us an article about why you did what you did.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89669 Visits: 14997
I definitely agree with Steve that you should post your choice. Especially why you chose that route.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search