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 |
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-03-12 : 06:30:35
|
Hey Guys I know this question has probably been asked a thousand times on this forumm, but I had a look and been searching on the net for an answer, and no one answer suits my situation.I have a Stored proc that handles paging on the server sideeverything works fine, until I get to the dynamic sorting. SET NOCOUNT ON; Declare @StartIndex int; Set @PropCount=(SELECT COUNT(property_id ) FROM [dbo].[vwResidentialProperty] WHERE (@Area is null or parentid = @Area) and (@locality is null or CharIndex( [dbo].[vwResidentialProperty].locid, @Locality)>0) and askprice between isnull(@MinPrice,0) and isnull(@MaxPrice ,5000000) and bedrooms between isnull(@MinBed,0) and isnull(@MaxBed ,10) and (@PropType is null or charindex( [dbo].[vwResidentialProperty].propertytype,@proptype)>0) and floorarea between isnull(@MinFloor,0) and isnull(@MaxFloor,5000000)); /*Check to see if the Number of Properties returned is less than actual page size if it is then set the start Index to the Property Count */ if @PropCount < @Numrow begin set @StartIndex = 1; end; else begin set @StartIndex = (@PageIndex * @Numrow) + 1; end;with Property As( SELECT top 100 percent case @SortOrder when 0 then ROW_NUMBER() OVER (order by [dbo].[vwResidentialProperty].askprice Asc) when 1 then ROW_NUMBER() OVER (order by [dbo].[vwResidentialProperty].askprice desc) when 2 then ROW_NUMBER() OVER (order by [dbo].[vwResidentialProperty].dateadded desc) end as row, [dbo].[vwResidentialProperty].property_id , [dbo].[vwResidentialProperty].Address, [dbo].[vwResidentialProperty].askprice , [dbo].[vwResidentialProperty].dateadded, [dbo].[vwResidentialProperty].Saleterm ,[dbo].[vwResidentialProperty].locid, [dbo].[property].adbrief FROM [dbo].[vwResidentialProperty] inner join [dbo].[property] on [dbo].[property].property_id = [dbo].[vwResidentialProperty].property_id WHERE (@Area is null or parentid = @Area) and (@locality is null or CharIndex( [dbo].[vwResidentialProperty].locid, @Locality)>0) and [dbo].[vwResidentialProperty].askprice between isnull(@MinPrice,0) and isnull(@MaxPrice ,5000000) and [dbo].[vwResidentialProperty].bedrooms between isnull(@MinBed,0) and isnull(@MaxBed ,10) and (@PropType is null or charindex( [dbo].[vwResidentialProperty].propertytype,@proptype)>0) and [dbo].[vwResidentialProperty].floorarea between isnull(@MinFloor,0) and isnull(@MaxFloor,5000000) ) Select Property_ID, Address as 'Address', askprice as 'Price', adbrief as 'Description', Saleterm, dateadded FROM Property WHERE Row BETWEEN @Startindex and (@startindex + @Numrow )-1 order by case @SortOrder when 0 then askprice when 1 then askprice -1 --when 2 then dateadded -1 end; I need to be able to provide 3 different sorting options to the user.1 . Price Asc2. Price Desc3. Date added AscNow I can't find away of providing this criteria.At the moment the options are available to the user on screen via option buttons. I don't really want supply SQL code in the HTML of the page i.e <option value = "Price asc">I would prefer to pass an integer value through to the stored proc, and let the case clause handle it.I have tried puttin Price Asc in the case clause, but SQL complains.is there any other way of doing this?Kind Regards,Gary<A href="http://www.threenineconsulting.com" target="_blank" >My Website</a> || <a href="http://threenineconsulting.com/forum/blogs/cykophysh/default.aspx" targer="_blank">My Blog</a> |
|
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-03-12 : 07:06:38
|
| I seemed to have resolved my problem with the following code,however I am open to any better solutions Regards,GarySET NOCOUNT ON; Declare @StartIndex int; Set @PropCount=(SELECT COUNT(property_id ) FROM [dbo].[vwResidentialProperty] WHERE (@Area is null or parentid = @Area) and (@locality is null or CharIndex( [dbo].[vwResidentialProperty].locid, @Locality)>0) and askprice between isnull(@MinPrice,0) and isnull(@MaxPrice ,5000000) and bedrooms between isnull(@MinBed,0) and isnull(@MaxBed ,10) and (@PropType is null or charindex( [dbo].[vwResidentialProperty].propertytype,@proptype)>0) and floorarea between isnull(@MinFloor,0) and isnull(@MaxFloor,5000000)); /*Check to see if the Number of Properties returned is less than actual page size if it is then set the start Index to the Property Count */ if @PropCount < @Numrow begin set @StartIndex = 1; end; else begin set @StartIndex = (@PageIndex * @Numrow) + 1; end;with Property As( SELECT top 100 percent case @SortOrder when 0 then ROW_NUMBER() OVER (order by [dbo].[vwResidentialProperty].askprice desc) when 1 then ROW_NUMBER() OVER (order by [dbo].[vwResidentialProperty].askprice asc) when 2 then ROW_NUMBER() OVER (order by [dbo].[vwResidentialProperty].dateadded desc) end as row, [dbo].[vwResidentialProperty].property_id , [dbo].[vwResidentialProperty].Address, [dbo].[vwResidentialProperty].askprice , [dbo].[vwResidentialProperty].dateadded, [dbo].[vwResidentialProperty].Saleterm ,[dbo].[vwResidentialProperty].locid, [dbo].[property].adbrief FROM [dbo].[vwResidentialProperty] inner join [dbo].[property] on [dbo].[property].property_id = [dbo].[vwResidentialProperty].property_id WHERE (@Area is null or parentid = @Area) and (@locality is null or CharIndex( [dbo].[vwResidentialProperty].locid, @Locality)>0) and [dbo].[vwResidentialProperty].askprice between isnull(@MinPrice,0) and isnull(@MaxPrice ,5000000) and [dbo].[vwResidentialProperty].bedrooms between isnull(@MinBed,0) and isnull(@MaxBed ,10) and (@PropType is null or charindex( [dbo].[vwResidentialProperty].propertytype,@proptype)>0) and [dbo].[vwResidentialProperty].floorarea between isnull(@MinFloor,0) and isnull(@MaxFloor,5000000) ) Select Property_ID, Address as 'Address', askprice as 'Price', adbrief as 'Description', Saleterm, dateadded FROM Property WHERE Row BETWEEN @Startindex and (@startindex + @Numrow )-1 end;Kind Regards,Gary<A href="http://www.threenineconsulting.com" target="_blank" >My Website</a> || <a href="http://threenineconsulting.com/forum/blogs/cykophysh/default.aspx" targer="_blank">My Blog</a> |
 |
|
|
|
|
|
|
|