Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case Statement

Author  Topic 

mikecro
Starting Member

21 Posts

Posted - 2010-01-14 : 11:32:25
I'm trying to use a query for both a search function and another query function with parameters taken from controls. At the end of the query, I'm trying to use a CASE function to determine the where clause, but I keep getting a syntax error related to the CASE function, but I can't see the problem. Can anyone see it or am I way off here?

Thanks!


ERROR: Incorrect syntax near the keyword 'CASE'.



select a.assignment_id,
s.student_id,
s.student,
e.elective,
a.isLottery,
a.status,
b.start_dt,
b.end_dt,
s.coord_notes,
a.assign_dt
from
(select assignment_id,
block_id,
student_id,
status,
isLottery,
assign_dt
from assignments) as a
inner join
(select block_id,
elective_id,
start_dt,
end_dt
from block_schedule) as b
on a.block_id=b.block_id
inner join
(select elective_id,
elective_name as elective
from electives) as e
on b.elective_id=e.elective_id
inner join
(select student_id as student_id,
lastname + ', ' + firstname + ' ' + middlename as student,
isNULL(coord_notes,' ') as coord_notes,
active
from student where active=@active) as s
on a.student_id=s.student_id
CASE s.student
WHEN '' THEN
where ( status like '%' + @status + '%' and
year(start_dt) =@year and active=@active)
ELSE
where (s.student like '%' + @search + '%')
END
order by s.student, e.elective,b.start_dt

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 11:38:07
you cant use case like this. its just an expression which returns a single value conditionally. thw where condition should be as below without any case statements:-


where (( status like '%' + @status + '%' and
year(start_dt) =@year and active=@active and s.student='')
or (s.student like '%' + @search + '%'))
Go to Top of Page

mikecro
Starting Member

21 Posts

Posted - 2010-01-14 : 11:50:55
Thank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 11:52:43
welcome
Go to Top of Page
   

- Advertisement -