473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"Dynamic" sorting inside a procedure

Hello,
I need to have a stored procedure, which performs sorting. Something like
this:

CREATE PROCEDURE procname
@sortby varchar(30)
AS
BEGIN
SELECT some, columns
FROM some_table
ORDER BY @sortby
END

(of course, i know this won't work, but it gives the idea of what i mean)
Is there a possibility to write a procedure which behaves like that? It is
important for me not to have multiple procedures just for different sorting
criteria...

Thanks,
Mike
Jul 20 '05 #1
3 1748
One method is to build and execute a dynamic SQL statement. For example:

EXEC
(
'SELECT some, columns
FROM some_table
ORDER BY ' + @sortby
)

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Michal Grabowski" <mg******@elka.pw.edu.pl> wrote in message
news:cf**********@julia.coi.pw.edu.pl...
Hello,
I need to have a stored procedure, which performs sorting. Something like
this:

CREATE PROCEDURE procname
@sortby varchar(30)
AS
BEGIN
SELECT some, columns
FROM some_table
ORDER BY @sortby
END

(of course, i know this won't work, but it gives the idea of what i mean)
Is there a possibility to write a procedure which behaves like that? It is
important for me not to have multiple procedures just for different sorting criteria...

Thanks,
Mike

Jul 20 '05 #2
Michal Grabowski (mg******@elka.pw.edu.pl) writes:
CREATE PROCEDURE procname
@sortby varchar(30)
AS
BEGIN
SELECT some, columns
FROM some_table
ORDER BY @sortby
END

(of course, i know this won't work, but it gives the idea of what i
mean) Is there a possibility to write a procedure which behaves like
that? It is important for me not to have multiple procedures just for
different sorting criteria...


http://www.sommarskog.se/dynamic_sql.html gives some suggestions.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Thanks for all tips!

Regards,
Mike
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Buddy Robbins | last post by:
Hey folks, I am re-writing an ASP.Net site for a company. The client wants to be responsible for the static content and navigation of their site, and I'll be responsible for any dynamic content. ...
2
by: Randy Yates | last post by:
Having done a bit of Access Basic programming, I'm realizing that AB does seem to have (as much as I hate to admit it since I think it's a toy language) an advantage over C++. Let's say I have a...
1
by: Razzbar | last post by:
I'm creating a site that is all on one page. The navigation of the site is enabled by a hidden iframe that fetches content from the server and re/places it in a div on the page. The problem is...
0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by XSDObjectGen. The hierarchy goes like this:...
2
by: Paul E Collins | last post by:
I'm considering using the NetSpell spell checking engine in my application. However, NetSpell is licensed under the Lesser GPL, and I want to withhold my application's source code. The licence...
5
by: Brad | last post by:
All samples related to this see to come short of being 'truly' dynamic. For instance, after creating all the code to load/save a properties value, you turn around and save it to one you KNOW...
1
by: Nathan Sokalski | last post by:
When testing a form of mine which uses RequiredFieldValidators that have the Display property set to "Dynamic" the ErrorMessage property is automatically removed when an entry is completely typed...
3
by: Good Man | last post by:
Hi there Ideally, I'd like to create one javascript function and pass the file extension i'm looking for to see if its there: <input type="file" onchange="checkFile('pdf',this)" /> and then...
0
by: walterbyrd | last post by:
This according to SDTimes: http://www.sdtimes.com/article/story-20071215-13.html They don't specifically mention Python. But, I think Python qualifies as a dynamic language. "1. Dynamic...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.