On 6 Jan 2005 08:51:35 -0800,
shyner@gmail.com wrote:
[color=blue]
>Hi Everyone,
>
>I've been battling this for two days with no luck. I'm using SQL
>Server 2000.
>
>Here's the mystery: I've got a stored procedure that takes a single
>varchar parameter to determine how the result set is sorted. Here it
>is:
>
>CREATE PROCEDURE spDemo @SortField varchar(30)
>
>AS
>SELECT dtmTimeStamp, strEmpName, strOld, strNew, strActionDesc
>FROM ActivityLog
>ORDER BY CASE @SortField
>WHEN 'dtmTimeStamp' THEN dtmTimeStamp
>WHEN 'strEmpName' THEN strEmpName
>WHEN 'strOld' THEN strOld
>WHEN 'strNew' THEN strNew
>WHEN 'strActionDesc' THEN strActionDesc
>END
>GO[/color]
(snip)
Hi Jimmy,
The explanation Ramesh gives you is correct. However, instead of manually
converting all data to the same datatype (which is often clumsy and can
even make proper sorting impossible), you should use several CASE
statement:
ORDER BY CASE WHEN @SortField = 'TimeStamp' THEN TimeStamp END,
CASE WHEN @SortField = 'EmpName' THEN EmpName END,
....
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)