Solved

Dynamic SQL

Posted on 2014-01-09
4
208 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

11 Experts available now in Live!

Get 1:1 Help Now