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-03-16 : 06:59:49
|
| I have the following sp. Some of the sort fields will be empty. Will these always be put in the same order if I sort ASC - because they aren't ! I wondered if it's a bug in my sp logic.CREATE PROCEDURE spRMU_GetFilesforRequestFiltered1@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 BYCASEWHEN @strSortString='SubjectAZ' then Subject WHEN @strSortString='FileDescriptionAZ' then FileDescription WHEN @strSortString='FileRefAZ' then FileRefelse Subject END ASCENDGO |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-03-16 : 07:27:04
|
well they will be orderd ascending wise... if you have 10 rows with null, and 5 with value "1" then the order willl be first the 10 null rows then the 5 "1" rows. the orders of the 10 or 5 rows isn't guarnateed to be always the same.you'd need to sort on another column for that.Go with the flow & have fun! Else fight the flow |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2005-03-16 : 07:34:56
|
Thanks Spirit1. I thought that they may be in the order they appeared in the sql table if they were all nulls/empty |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-03-16 : 08:57:05
|
| Worthwhile always including the primary key fields in a sort that has duplciates - that way the ordering is "repeatable" - otherwise although they may appear in the same order each time other activities (such as rebuilding indexes etc.) may cause them to display in a different orderKristen |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2005-03-16 : 09:01:57
|
| Do you meanORDER BY FIELD1, FIELD2 where FIELD2 is the key of the table ? |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-03-16 : 09:36:52
|
| Exactly - if you've got several Primary Key fields you would doORDER BY DataColumn1, DataColumn2, PKField1, PKField2 ...Kristen |
 |
|
|
|
|
|
|
|