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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 25th, 2004, 04:16 PM
srdiesel srdiesel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 37 srdiesel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 43 sec
Reputation Power: 5
Using variables in Stored Procedure

I am writing a stored procedure that basically gets a whole lot of info. Simple enough... that part works. Now I want to add functionality to pass in sortBy and direction variables so the results can be sorted from a web page. The code I have is:

Code:
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


ALTER   procedure GetRecruiterApplicants
	@userid	varchar(50),
	@trackerGroup varchar(50),
	@sortBy varchar(20),
	@dir varchar(5)

as
begin

set nocount on

SELECT C.USERID INTO #VU FROM VUSERS V WITH (NOLOCK), COMPANIES C WITH (NOLOCK) 
WHERE CHARINDEX(','+C.USERID+',', ','+VUSERS+',')>0 AND V.USERID=@userid
UNION SELECT @userid

SELECT distinct l.AccessCode, l.ApplicantGivenName, l.ApplicantFamilyName, l.DateCreated, l.DateApplicantAccessed, p.ApplicationTitle, l.disabled
FROM chamslinks l, chamsProjectIDs p, chamsGroups g 
WHERE l.TrackerID IN (SELECT userid FROM #VU) 
	AND g.trackerGroup = @trackerGroup 
	AND p.groupTblID = g.groupTblID 
	AND p.ProjectID = l.ProjectID
ORDER BY l.@sortBy @dir

DROP Table #VU

set nocount off
end


The error I am getting is: "Server: Msg 170, Level 15, State 1, Procedure GetRecruiterApplicants, Line 24
Line 24: Incorrect syntax near '@dir'."

Any help?

Reply With Quote
  #2  
Old August 25th, 2004, 04:30 PM
Blase Blase is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 Blase User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You cannot directly use the variable in the ORDER BY. I've found the CASE statements quite useful (if cumbersome and ugly) in such cases.
...ORDER BY CASE
WHEN @SortDir = 'A' THEN <table>.A
WHEN @SortDir = 'B' THEN <table>.B
...
END


-Blasé

Reply With Quote
  #3  
Old August 25th, 2004, 04:41 PM
srdiesel srdiesel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 37 srdiesel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 43 sec
Reputation Power: 5
Question

2 things:

1) Is that syntax right b/c when I enter it into the query analyzer, there seems to be an error. Do I have to terminate the statement in any way?
2) Can I store the the 2nd SELECT statement as a string, build it, and then execute the SQL?

Last edited by srdiesel : August 25th, 2004 at 04:43 PM. Reason: Wrong wording.

Reply With Quote
  #4  
Old August 26th, 2004, 11:15 AM
Blase Blase is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 Blase User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Re 1),
Assuming that you took my code sample verbatim and tried to run it, the <table> bits are placeholders for whatever your table name may be, while the 'A' and 'B' and ellipsis (...) indicate your field names, whatever and however many they be.

Re 2),
Yes, you can. But should you? There are performance impacts to be considered for each method, and I would recommend some research with SQL Books Online and http://www.sql-server-performance.com before you commit yourself.

-Blasé

Quote:
Originally Posted by srdiesel
2 things:

1) Is that syntax right b/c when I enter it into the query analyzer, there seems to be an error. Do I have to terminate the statement in any way?

2) Can I store the the 2nd SELECT statement as a string, build it, and then execute the SQL?

Reply With Quote
  #5  
Old August 26th, 2004, 11:51 AM
srdiesel srdiesel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 37 srdiesel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 43 sec
Reputation Power: 5
In response

In response to your response to me question 2. The size of the recordset being returned from the query is very small and will not scale to a large number. Because of this, I dont think its really necessary to worry about optimiztion. Would you agree?

Reply With Quote
  #6  
Old August 26th, 2004, 05:09 PM
Blase Blase is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 Blase User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Seems like you know what you're talking about. I can't say just how a variable ORDER BY clause would affect optimization, but as long as the query's balanced (WITH RECOMPILES et al), I suppose you should be alright.

-Blasé

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Using variables in Stored Procedure


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