Solved

Stored proc Sort by Variable

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell GUI pull downs populated by MS SQL table 2 36
LTrim & Double Space Correction 5 40
job schedule 8 17
sql 2016 data tools breakdown.. 1 13
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 …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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
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.

839 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