Solved

Stored proc Sort by Variable

Posted on 1997-11-01
2
636 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

759 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

19 Experts available now in Live!

Get 1:1 Help Now