February 26, 2010 at 2:11 am
I have a set of rows to be fetched from a table sorted on the column name passed in the stored procedure.
Here is the code which i used:
Declare @ordercol varchar(100),@sql as varchar(500)
set @sql ='select top 5 *
from sys.objects
order by '+@ordercol
exec(@sql)
Is it possible to do this without using dynamic query?
--Divya
February 26, 2010 at 2:32 am
You could use the CASE statement. Something like:
DECLARE @ordercol sysname,@sql AS VARCHAR(500)
SET @ordercol='object_id'
SELECT TOP 5 *
FROM sys.objects
ORDER BY
CASE @ordercol
WHEN 'object_id' THEN STR(OBJECT_ID)
WHEN 'name' THEN name
ELSE CONVERT(CHAR(10),create_date,120)
END
It's important to convert all possible CASE values to the same data type. If you'd use for instance OBJECT_ID instead of STR(OBJECT_ID) you'd get an conversion error.
February 26, 2010 at 3:01 am
Thanks Lutz.
Your solution i have already used for less number of columns.
There are lots of columns around 100's in the table which i need to query. Case will be really hard to write for all 100 columns.
Is there any other good alternative?
--Divya
February 26, 2010 at 3:09 am
Divya Agrawal (2/26/2010)
Thanks Lutz.Your solution i have already used for less number of columns.
There are lots of columns around 100's in the table which i need to query. Case will be really hard to write for all 100 columns.
Is there any other good alternative?
Thank you for providing this important information. Would have helped a lot if included in the first post. :angry:
You could use dynamic SQL ,meaning: build the SELECT statement as a string and call that with sp_executesql. See BOL for details.
February 26, 2010 at 3:55 am
lmu92 (2/26/2010)
You could use dynamic SQL ,meaning: build the SELECT statement as a string and call that with sp_executesql. See BOL for details.
Thanks again.
I have already written in my first post that is is possible without dynamic query.
--Divya
February 26, 2010 at 8:46 pm
What is the format of @ordercol?
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
February 26, 2010 at 8:48 pm
also, please give us the reasons for not wanting to use Dynamic SQL, don't make us grope around in the dark.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
February 27, 2010 at 6:57 am
Divya Agrawal (2/26/2010)
Is it possible to do this without using dynamic query?
It is, but it is difficult to get it right, and more importantly, to get it to stay that way.
Use dynamic SQL. It's better in the long run, and you get used to all the red text.
Coding options for dynamic search conditions: http://www.sommarskog.se/dyn-search-2005.html
The curse and blessings of dynamic SQL:http://www.sommarskog.se/dynamic_sql.html
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply