Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2005-02-24 : 07:02:19
|
| I am passing variables to my sp to control the 'order by'. I also want to pass whether the the data is sorted ASC or DESC. Here's my sp, but it's not working. If I remove @strSortOrder it works. Thanks for any help.CREATE PROCEDURE spRMU_GetFilesforRequestFiltered@strDepartment nvarchar(100),@strDivision nvarchar(100),@strSection nvarchar(100),@strFileRef nvarchar(100),@strSubject nvarchar(1000),@strDescription nvarchar(1000),@strBoxNo nvarchar(10),@strConNo nvarchar(10),@strViewRestricted bit,@strSortString nvarchar(200),@strSortOrder nvarchar(8) ASIF @strViewRestricted = 1BEGINSELECT *FROM tblFilesWHEREDept = @strDepartment andDivision =@strDivision andSect like '%'+@strSection+'%' andFileRef like '%' + @strFileRef + '%' andSubject like '%'+@strSubject+'%' andFileDescription like '%'+@strDescription+'%' andBoxNo like '%'+cast(@strBoxNo as varchar(50))+'%' andConNo like '%'+cast(@strConNo as varchar(50))+'%'ORDER BYCASE WHEN @strSortString='SubjectAZ' then Subject WHEN @strSortString='FileDescription' then FileDescription+ @strSortOrder END |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2005-02-24 : 07:34:19
|
| This should do it:...ORDER BY CASE WHEN @strSortOrder = 'DESC' THEN null ELSECASE WHEN @strSortString='SubjectAZ' then Subject WHEN @strSortString='FileDescription' then FileDescription ENDEND ASC,CASE WHEN @strSortOrder = 'ASC' THEN null ELSECASE WHEN @strSortString='SubjectAZ' then Subject WHEN @strSortString='FileDescription' then FileDescription ENDEND DESCDon't know why you need nvarchar for those two parameters, or why you need 8 characters to store "ASC" or "DESC". |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2005-02-24 : 07:38:58
|
Thanks for your help - to answer your questions, cos I'm a newbie to all this ! |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2005-02-24 : 10:33:59
|
| It's not sorting anything.....CREATE PROCEDURE spRMU_GetFilesforRequestFiltered@strDepartment nvarchar(100),@strDivision nvarchar(100),@strSection nvarchar(100),@strFileRef nvarchar(100),@strSubject nvarchar(1000),@strDescription nvarchar(1000),@strBoxNo nvarchar(10),@strConNo nvarchar(10),@strViewRestricted bit,@strSortString nvarchar(200),@strSortOrder nvarchar(4) ASIF @strViewRestricted = 1BEGINSELECT *FROM tblFilesWHEREDept = @strDepartment andDivision =@strDivision andSect like '%'+@strSection+'%' andFileRef like '%' + @strFileRef + '%' andSubject like '%'+@strSubject+'%' andFileDescription like '%'+@strDescription+'%' andBoxNo like '%'+cast(@strBoxNo as varchar(50))+'%' andConNo like '%'+cast(@strConNo as varchar(50))+'%'ORDER BY CASE WHEN @strSortOrder = 'DESC' THEN null ELSECASE WHEN @strSortString='SubjectAZ' then Subject WHEN @strSortString='FileDescription' then FileDescription ENDEND ASC,CASE WHEN @strSortOrder = 'ASC' THEN null ELSECASE WHEN @strSortString='SubjectAZ' then Subject WHEN @strSortString='FileDescription' then FileDescription ENDEND DESCENDGO |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-02-24 : 11:01:38
|
| are you passing in the correct parameters? how are you calling it? Should SubjectAZ for descending be SubjectZA ?- Jeff |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2005-02-24 : 11:14:16
|
mmmm I think its lack of communication between fingers and brain - sorry. Typos again |
 |
|
|
|
|
|
|
|