Solved

Stored proc Sort by Variable

Posted on 1997-11-01
2
644 Views
Last Modified: 2008-03-06
I would like to have a stored procedure result set sorted by  a field I pass to the procedure as a parameter.  I would also like to have the sort direction (asc/desc) determined by a parameter.  I believe that to make this work it must be in a precompiled procedure and must specify the field name and not the column number because I've read this in documentation.  I've tried every way I can think of and am wondering if I have to specify special pre-compile or binding rules on the procedure?

Here is my test code:

use pubs
go
drop proc kentest
go

create proc kentest (@vsort varchar(8)='au_lname')
as
select * from authors
order by @vsort

go
exec kentest @vsort='au_lname'

The error message I get is:
Msg 1008, Level 15, State 1
The select list item identified by the order by number '1' contains a variable as part of the expression that identifies a column position. Variables are only allowed when ordering by an expression referencing a column name.
0
Comment
Question by:kfenske
2 Comments
 
LVL 1

Accepted Solution

by:
mikkon earned 100 total points
ID: 1089044
The easiest way to do this is to write your procedure so that it constructs the whole SQL statement and then EXECs it. In your case:

CREATE PROC kentest (@vsort varchar(8)='au_lname')
AS

DECLARE @exestr varchar(255)
SELECT @exestr='select * from authors order by ' + @vsort
EXEC @exestr
GO

Of course, there is a small performance penalty since the @exestr has to be compiled every time the procedure is run.

0
 

Author Comment

by:kfenske
ID: 1089045
Excellent answer!

Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 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

16 Experts available now in Live!

Get 1:1 Help Now