MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2004, 01:49 PM
miken miken is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Hinsdale, MA; Shrewsbury VT.
Posts: 3 miken User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
T-SQL Help with Order by

In a stored procedure MS-SQL I am trying to write an order by expression that is a function of the incoming paramters.

SO for example

CREATE PROCEDURE test
@sortby varchar(10)
AS

SELECT * FROM table WHERE condition
ORDER BY @SORTBY

Gives an error (1008) but the error message implies that you can use a variable in the order by expression.

I have tried every combination I can think of

with the variable as varchar = column name, as integer .

I have also tried variations of
Order by COL_Name(OBJECT_ID('item"), variable) and I ether get errors or no errors but no order either.

Any leads appreciated.

TIA

Mike

Reply With Quote
  #2  
Old December 14th, 2004, 10:24 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,967 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 59 m 30 sec
Reputation Power: 1024
you need to use the EXECUTE command for a dynamic query
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #3  
Old December 14th, 2004, 12:14 PM
miken miken is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Hinsdale, MA; Shrewsbury VT.
Posts: 3 miken User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
you need to use the EXECUTE command for a dynamic query


Got it, thanks

Reply With Quote
  #4  
Old December 16th, 2004, 06:04 AM
lvramani lvramani is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 lvramani User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by miken
In a stored procedure MS-SQL I am trying to write an order by expression that is a function of the incoming paramters.

SO for example

CREATE PROCEDURE test
@sortby varchar(10)
AS

SELECT * FROM table WHERE condition
ORDER BY @SORTBY

Gives an error (1008) but the error message implies that you can use a variable in the order by expression.

I have tried every combination I can think of

with the variable as varchar = column name, as integer .

I have also tried variations of
Order by COL_Name(OBJECT_ID('item"), variable) and I ether get errors or no errors but no order either.

Any leads appreciated.

TIA

Mike


This problem can be solved by executing the Query Dynamically

Rewrite the Stored procedure as follows

CREATE PROCEDURE test
@sortby varchar(10)
AS
Declare @QString nvarchar(500)
/* The @SORTBY Must be a valid Column Name*/

Set @QString = 'SELECT * FROM table WHERE condition Order by ' + @SORTBY

/*sp_ExecuteSql - This is a Sql Server Built in Stored Procedure. this is used to execute a query string as a query */
exec sp_ExecuteSql @QString

--- Hope this solves your problem

Happy Programming

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > T-SQL Help with Order by


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 2 hosted by Hostway
Stay green...Green IT