December 4, 2007 at 4:04 pm
Invalid syntax near the keyword 'case'.
SELECTROW_NUMBER()
OVER (
case @sort
when 'relevance' then ORDER BY hits DESC
else ORDER BY name ASC
end
) AS row ....
I want the OVER sorting to be either ORDER BY hits DESC or ORDER BY name ASC depending on the value of @sort varchar(50), WITHOUT using dynamic queries.
How can I do something like this?
December 4, 2007 at 4:11 pm
I guess it's a wrong forum for this question.
SQL Server 7,2000 don't support such constructions.
_____________
Code for TallyGenerator
December 5, 2007 at 10:33 am
Thanks Ramesh, works out great.
I have also been trying something like:
select * from Listings where lis_name=@lis_name
(
case when @franchise is not null then ' AND lis_franchise=@lis_franchise'
)
What I want to do is if it sees a non-null @lis_franchise parameter, I want to append " AND lis_franchise=@lis_franchise" to the WHERE clause...again without using dynamic queries. Is this also possible?
December 5, 2007 at 1:58 pm
No - not like that.
CASE is used to return single scalar values, but not to add clauses to a SQL statement (or to add to a clause).
You need dynamic SQL of some sort for that, or you need to use the case a little differently.
something like below
select *
from
Listings
where
lis_name=@lis_name
and isnull(@franchise,-999)=case when @franchise is not null then lis_franchise else -999 end
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 5, 2007 at 2:53 pm
Matt, I get a "Conversion failed when converting the varchar value 'some franchise' to data type int." error with your case statement. Again I like to keep the solution using static queries if at all possible.
Thanks
Ham
December 5, 2007 at 3:58 pm
I made an assumption as to what kind of data @franchise was. if it's supposed to be some kind of string, put quotes around the -999 (both instances), which I just picked as some random "not likely to occur" value in an identity field.
in other words:
select *
from
Listings
where
lis_name=@lis_name
and isnull(@franchise,'-999')=case when @franchise is not null then lis_franchise else '-999' end
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 5, 2007 at 4:12 pm
Great, it worked, but can you explain to me exactly what's going on here:
isnull(@franchise,'-999')=case when @franchise is not null then lis_franchise else '-999' end
What does isnull(@franchise,'-999')= mean and what is the -999 for?
Thanks,
Ham
December 5, 2007 at 4:17 pm
If to assume that "hits" is a positive integer number then this should work:
SELECT {blah}
FROM {sometable}
ORDER BY CASE WHEN @sort = '{whatever}' THEN STR(2147483647-hits, 10) ELSE name END ASC
_____________
Code for TallyGenerator
December 5, 2007 at 4:21 pm
ISNULL(a,b) is a function. It check to see if A is null: if it isn't - it returns A, otherwise B. I was just using it as an arbitrary value, so that when @franchise happens to be null, your where ends up essetially performing this query:
...
where
lis_name=@lis_name
and -999=-999
the last line is always true, so it's functionally the same as if it wasn't there. it's a way to "cancel" out a criteria in the WHERE statement.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 5, 2007 at 11:23 pm
Although, Matt's solution do provide the results you wanted, but it uses functions in WHERE clause which can be avoided. Using functions in WHERE clauses is not always scalable in production as it forces optimizer to exclude the appropriate indexes, if any available for it.
Here is the code which would also give you the same results....
SELECT*
FROMListings
WHERElis_name = @lis_name
AND
(
( @franchise IS NULL ) OR ( lis_franchise = @franchise )
)
--Ramesh
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply