Solved

Stored proc Sort by Variable

Posted on 1997-11-01
2
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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