Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 18th, 2003, 10:07 AM
akmolteni akmolteni is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 1 akmolteni User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Stored Procedure: updating the "order by" clause

I have a stored procedure that returns a recordset which I would like to have sorted based on the inputs. For example, a product listing could be ordered by product number, product name, or price.

I have tried creating the whole SQL query in a variable and executing it, only to get an error because it can't find the stored procedure.

What I'm looking for is something like (pseudocode)

select * from Products where

if SortByPrice is true then
order by Products.Price ASC
end if
if SortByName is true then
order by Products.Name ASC
end if

and so on.

Thanks.

Reply With Quote
  #2  
Old August 18th, 2003, 11:17 PM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 6
hey akmolteni

Try:
Code:
CREATE PROC sp_Data
   (
   @SortBy int
   )
AS

SELECT Table1.* FROM Table1
ORDER BY 
      CASE @SortBy WHEN 1 THEN Field1 END,
      CASE @SortBy WHEN 2 THEN Field2 END,
      CASE @SortBy WHEN 3 THEN Field3 END,
      CASE @SortBy WHEN 4 THEN Field4 END
, Field5, Field6



Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Stored Procedure: updating the "order by" clause


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway