473,389 Members | 1,098 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,389 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 1747
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.