mysql - Error in nested case query using SQL -
trying convert below query sql
, query works fine on mysql
. problem seems case when
area field same error.
show msg 102, level 15, state 1, line 44 incorrect syntax near '='. msg 156, level 15, state 1, line 47 incorrect syntax near keyword 'and'. msg 156, level 15, state 1, line 49 incorrect syntax near keyword 'else'.
when t.[statusid] = 3 case when (((select count(ta1.[approver_id]) [qestorm].[dbo].[cr_ticketapproval] ta1 inner join [qestorm].[dbo].[cr_controlflow_subroute] cfsr1 on ta1.[subroute_id] = cfsr1.[id] ta1.[ticket_id]= @itkid , ta1.active=1 , cfsr1.active=1 , cfsr1.[sequence] =(select cfsr2.[sequence] [qestorm].[dbo].[cr_ticket] t2 inner join [qestorm].[dbo].[cr_controlflow_subroute] cfsr2 on t2.[subrouteid] = cfsr2.[id] t2.[id] = @itkid))<(select count(distinct cfsr1.[id])from [qestorm].[dbo].[cr_ticket] t1 inner join [qestorm].[dbo].[cr_controlflow_route] cfr1 on t1.[formid] = cfr1.[formid] inner join [qestorm].[dbo].[cr_controlflow_subroute] cfsr1 on cfr1.[id] = cfsr1.[routeid] cfr1.[active] = 1 , cfsr1.[active] = 1 , t1.[id] = @itkid , cfsr1.[category] = 1 , cfsr1.[sequence] = ( select cfsr2.[sequence] [qestorm].[dbo].[cr_ticket] t2 inner join [qestorm].[dbo].[cr_controlflow_subroute] cfsr2 on t2.[subrouteid] = cfsr2.[id] t2.[id] = @itkid)))) case when ((select count(1) [qestorm].[dbo].[cr_ticketapproval] [ticket_id]=@itkid , [active]=1) = 0) --error show here => ((t.[audituser_id] = '444' or t.[audituser_id] null) , (nx.actor = 2 or appsameseq.ntlogin=in_ntlogin) , nx.actor=3 , srsameseq.subrouteid not in (select subrouteid [qestorm].[dbo].[cr_ticketapproval] [ticket_id]=@itkid , active=1 ) , appsameseq.ntlogin=in_ntlogin , nx.actor=3 , srsameseq.subrouteid not in (select subrouteid [qestorm].[dbo].[cr_ticketapproval] [ticket_id] = @itkid , active = 1) else 0 end
i'll toss hat in ring.
there may more 1 thing wrong sql statement. i'll point out this:
case when ((select count(1) [qestorm].[dbo]. [cr_ticketapproval] [ticket_id]=@itkid , [active]=1) = 0) --error show here => ((t.[audituser_id] = '444' or t.[audituser_id] null) , (nx.actor = 2 or appsameseq.ntlogin=in_ntlogin) , nx.actor=3 , srsameseq.subrouteid not in (select subrouteid [qestorm].[dbo].[cr_ticketapproval] [ticket_id]=@itkid , active=1 ) , appsameseq.ntlogin=in_ntlogin , nx.actor=3 , srsameseq.subrouteid not in (select subrouteid [qestorm].[dbo].[cr_ticketapproval] [ticket_id] = @itkid , active = 1) else 0 end
are trying evaluate conditional expression, , return result 1 or 0, if boolean expression in programming language?
that doesn't work in tsql. kind of expression evaluation:
set @value = (1 > 0)
... produce error. can't evaluate conditional expression: can use in test, in where
, having
, or when
clause.
so, if that's you're doing, might better wrap conditional evaluation in yet case
statement, this:
then case when {complex conditional statement} 1 else 0 end else 0 end
one other thing: extremely complex query statement! haven't analyzed enough see whether simplified, i'd suggest so, eye toward using common table expressions in place of of subqueries. can make query lot easier understand (and debug).
Comments
Post a Comment