Sunday, March 25, 2012

Does Sql 2005 allow ORDER BY @Variable ?

I am writing a stored proc and do not want to use dynamic sql. Does Sql
2005 allow ORDER BY @.Variable? The query compiler accepts it, but it
doesn't work. My @.Variable is a VARCHAR(50).
Thanks!This is a multi-part message in MIME format.
--=_NextPart_000_0924_01C71867.3AEC95F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Not directly. You could use a CASE structure to allow alternative =orderings. Here is one idea:
USE Northwind
GO
DECLARE @.OrderVar varchar(20)
SET @.OrderVar =3D 'LastName'
SELECT LastName,
FirstName
FROM Employees
ORDER BY CASE @.OrderVar
WHEN 'LastName' THEN LastName
WHEN 'FirstName' THEN FirstName
END ASC,
CASE @.OrderVar
WHEN 'LastName' THEN FirstName
WHEN 'FirstName' THEN LastName
END ASC
-- Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience. Most experience comes from bad judgment. - Anonymous
You can't help someone get up a hill without getting a little closer to =the top yourself.
- H. Norman Schwarzkopf
"Dan E" <dan_english2@.cox.net> wrote in message =news:u1AfegKGHHA.1280@.TK2MSFTNGP04.phx.gbl...
>I am writing a stored proc and do not want to use dynamic sql. Does =Sql > 2005 allow ORDER BY @.Variable? The query compiler accepts it, but it > doesn't work. My @.Variable is a VARCHAR(50).
> > Thanks!
> >
--=_NextPart_000_0924_01C71867.3AEC95F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Not directly. You could use a CASE =structure to allow alternative orderings. Here is one idea:
USE =NorthwindGO
DECLARE @.OrderVar =varchar(20)SET @.OrderVar =3D 'LastName'
SELECT LastName, FirstNameFROM EmployeesORDER BY CASE @.OrderVar = WHEN 'LastName' THEN LastName &=nbsp; WHEN 'FirstName' THEN FirstName END ASC, CASE @.OrderVar = WHEN 'LastName' THEN FirstName = WHEN 'FirstName' THEN LastName END ASC
-- Arnie Rowland, =Ph.D.Westwood Consulting, Inc
Most good judgment comes from =experience. Most experience comes from bad judgment. - Anonymous
You can't help someone get up a hill =without getting a little closer to the top yourself.- H. Norman Schwarzkopf
"Dan E" =wrote in message news:u1AfegKGHHA.1280@.TK2MSFTNGP04.phx.gbl...>I =am writing a stored proc and do not want to use dynamic sql. Does Sql > =2005 allow ORDER BY @.Variable? The query compiler accepts it, but it => doesn't work. My @.Variable is a VARCHAR(50).> > Thanks!> >

--=_NextPart_000_0924_01C71867.3AEC95F0--|||http://databases.aspfaq.com/database/how-do-i-use-a-variable-in-an-order-by-clause.html
"Dan E" <dan_english2@.cox.net> wrote in message
news:u1AfegKGHHA.1280@.TK2MSFTNGP04.phx.gbl...
>I am writing a stored proc and do not want to use dynamic sql. Does Sql
>2005 allow ORDER BY @.Variable? The query compiler accepts it, but it
>doesn't work. My @.Variable is a VARCHAR(50).
> Thanks!
>

No comments:

Post a Comment