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 Error

Author  Topic 

DeveloperIQ
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 13:42:02
What is wrong with the statement
CASE
WHEN (Select COUNT(1) FROM dbo.udft_ReturnCharIDs(@cpt)) > 0 THEN
TM.TM_CPT_CODE IN (Select TheID FROM dbo.udft_ReturnCharIDs(@cpt))

END
I have this in the where clause and keep getting the following error while compiling my SP
Msg 156, Level 15, State 1, Procedure XXXX, Line 92
Incorrect syntax near the keyword 'IN'.
Msg 156, Level 15, State 1, Procedure XXXX, Line 94
Incorrect syntax near the keyword 'END'.
@cpt is of type varchar(8000)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 13:45:32
you dont need the case statement. what you need is this

...
INNER JOIN (Select TheID FROM dbo.udft_ReturnCharIDs(@cpt)) tmp
ON tmp.TheID=TM.TM_CPT_CODE
Go to Top of Page

DeveloperIQ
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 13:48:15
I do Visakh.. The reason is that the subquery wont always return data in which case I would dont it to not filter at all.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 13:49:56
then use left join

LEFT JOIN (Select TheID FROM dbo.udft_ReturnCharIDs(@cpt)) tmp
ON tmp.TheID=TM.TM_CPT_CODE
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 13:50:35
post your full query if you need full solution.
Go to Top of Page

DeveloperIQ
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 13:51:35
quote:
Originally posted by visakh16

then use left join

LEFT JOIN (Select TheID FROM dbo.udft_ReturnCharIDs(@cpt)) tmp
ON tmp.TheID=TM.TM_CPT_CODE



Ok This makes more sense. Dumb me. But just out of curiousity, do you see a problem with the above case statement?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 13:54:15
yup. thats not a place where you use case. you cant use case to return you different filter conditions. case is normally use to conditionally assign value to a column.
Go to Top of Page

DeveloperIQ
Yak Posting Veteran

71 Posts

Posted - 2008-10-30 : 13:55:11
Thanks Visakh!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 14:01:14
welcome
Go to Top of Page
   

- Advertisement -