|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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é |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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:
|
|
#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?
|
|
#6
|
|||
|
|||
|
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é |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Using variables in Stored Procedure |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|