Solved

Dynamic Select columns using SQL Server Stored Procedure

Posted on 2009-04-02
4
417 Views
Last Modified: 2012-05-06
I have to write a single stored procedure that returns data to a client application that allows users to build dynamic queries based on a variable mix of selection parameters.  The application presents the user with a check list of field names and the user ticks the fields which are to be included in the report, e.g.
[ ] Product Code
[ ] Product Description
[ ] Weight
[ ] Volume
[ ] Standard Price
[ ] Supplier
etc ..
(There are upwards of 100 different fields that the user may choose.)

Somehow I need to be able to pass the list of column names to a stored procedure and build the sql select statement within the stored procedure based on which columns have been selected by the user.  I also have to do it in a way that does not make the stored procedure wide open to an injection attack.

What's the best way of doing this?


0
Comment
Question by:ccravenbartle
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
udayakumarlm earned 500 total points
ID: 24048436
you can use the code to start with. the program has to call the procedure with comma separated values as parameter.
Procedurename '*' -- for all fields

Procedurename ' Product Code, Product Description, Weight, Volume, Standard Price, Supplier'  --for seleccted fields
create Proc Procedurename

@abc varchar(2000)

as

declare @str Nvarchar(1000)

set @str = 'select ' + @abc + ' from tablename'

print @str

Execute Sp_ExecuteSql @Str

Open in new window

0
 

Author Comment

by:ccravenbartle
ID: 24049841
Uday: Thank you for your response - I've tested it and it does work.  The points are yours.  

Before I close the question can you advise how I could get the stored procedure to parse the parameter string to check that each field in the string exists as a column in the table before executing the query.  That way I can ensure that no rubbish or malicious code gets built into the select script.
0
 
LVL 12

Expert Comment

by:udayakumarlm
ID: 24050917
that validation you have to put in the UI. parsing the columns and then using them will be costly. if you are asking the user to pass the values in a text box then it is dangarous, if you are using a list box and constructing the string in the application then there is no way the user will send some melicius code.
in situation like 'select ' + @abc + ' from tablename' we can not assure that the @abc will not have any non related code.
we did tried to find the words that need to be ignored in one of our applications, it will ignore the words delete,insert,update if they come from the user. that stoped us from useing the fieldnames like updatedDate,DateInsert, DeletedInformation etc.
so, use the UI to do the validatation.
0
 

Author Closing Comment

by:ccravenbartle
ID: 31565728
Uday - thank you for your solution and for your additional comments.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

12 Experts available now in Live!

Get 1:1 Help Now