Solved

Dynamic SQL

Posted on 2014-01-09
4
207 Views
Last Modified: 2014-01-17
I have 10 columns on a GUI interface the user can group by or sum by any of these 10 columns.

There are two listboxes one for group by and one for sum by.

The user can choose the columns to group by and also the columns to sum by.

I need to write a dynamic sql query to fetch the data based on the columns selected for grouping and sum.
Any examples would be great
0
Comment
Question by:countrymeister
4 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39769364
If you have a GUI interface, can you build the SQL there and send it to SQL Server?

Are you displaying those same 10 columns or are there other columns that are displaying?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39769431
Can you explain what the "sum by" is intended to do?
Do you have some "roll-up" capability in mind so that you achieve sub-totals and grand totals?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39774375
Pass the two columns from GUI interface to your database and you can construct a dynamic sql and execute.
You can create an SP and pass the parameters. check this code if this works for you.
DECLARE @GroupBy VARCHAR(100),@SumBy VARCHAR(100)
DECLARE @Sql VARCHAR(MAX)
SELECT @Sql = 'SELECT ' + @GroupBy + ',SUM(' + @SumBy + ') AS SumBy
                 FROM YourTable
				GROUP BY ' + @GroupBy
EXEC(@Sql)

Open in new window

0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 39789185
I would suggest not to use exec @sql , instead use Execute sp_Executesql
Where we can send parameters, also the query plan is cached
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now