2014-02-18 96 views
0

我的查詢看起來象下面這樣:SQL Server錯誤 - 不正確的語法附近,如果條件

declare @test varchar(500) 
SELECT @test=coalesce(@test+',','') + cast(RoleName as varchar) FROM                
     ( select roles.RoleName from LU_BCOMS_usersroles usrroles 
      inner join LU_BCOMS_roles roles 
      on roles.roleid  = usrroles.Roles_roleid 
      where Users_Userid='MV10310' 
     ) as Tbl 

select repfamily.ProductName as Category,repfamily.Family as SeqChange,repfamily.RepFamily as DescOfChange, req.*, 
TaskCompVer = 
CASE WHEN req.UpdateByASPM is not null THEN 'Provide PLQM Wish List' 
     WHEN req.UpdateByASPM is null THEN 'Provide ASPM Wish List' 
     WHEN req.CreatedBy is not null THEN 'Provide ASPM Wish List' 
     END 
    from performa.TX_BCOMS_Request as req 
inner join TX_BCOMS_Requestrepfamily family on 
family.request_requestid=req.requestid 
inner join LU_BCOMS_RepFamily as repfamily on 
family.RepFamily_repFamilyid=repfamily.repfamilyid 

where req.UpdatedByPLQM is null 
(
    if (CHARINDEX('ASPM',@test)> 0) 
     Begin 
     and req.UpdatedByASPM is null 
     end 
    else if (CHARINDEX('PLQM' ,@test)> 0) 
     begin 
     and req.UpdatedByASPM IS NOT null 
     END 
) 

我不能夠建立查詢它說如果附近的語法不正確任何一個可以幫助我從這個..

,如果我遇到這種情況,如果(CHARINDEX(「ASPM」,@測試)> 0),我需要在「和req.UpdatedByASPM爲空」,否則下一個加...

回答

1

我想你期待這個,,,

declare @test varchar(500) 
SELECT @test=coalesce(@test+',','') + cast(RoleName as varchar) FROM                
     ( select roles.RoleName from LU_BCOMS_usersroles usrroles 
      inner join LU_BCOMS_roles roles 
      on roles.roleid  = usrroles.Roles_roleid 
      where Users_Userid='MV10310' 
     ) as Tbl 

select repfamily.ProductName as Category,repfamily.Family as SeqChange,repfamily.RepFamily as DescOfChange, req.*, 
TaskCompVer = 
CASE WHEN req.UpdateByASPM is not null THEN 'Provide PLQM Wish List' 
     WHEN req.UpdateByASPM is null THEN 'Provide ASPM Wish List' 
     WHEN req.CreatedBy is not null THEN 'Provide ASPM Wish List' 
     END 
    from performa.TX_BCOMS_Request as req 
inner join TX_BCOMS_Requestrepfamily family on 
family.request_requestid=req.requestid 
inner join LU_BCOMS_RepFamily as repfamily on 
family.RepFamily_repFamilyid=repfamily.repfamilyid 

where req.UpdatedByPLQM is null and (

    ((CHARINDEX('ASPM',@test)> 0) and req.UpdatedByASPM is null) 
    or 
    ((CHARINDEX('PLQM' ,@test)> 0) and req.UpdatedByASPM IS NOT null) 
    ) 
+0

ü可以添加SQlFiddle請。 – sureshhh

0

試試這個WHERE子句地區

WHERE req.UpdatedByPLQM Is Null 
     And 
     (CASE 
      WHEN CHARINDEX('ASPM',@test)> 0 and req.UpdatedByASPM Is Null THEN 1 
      WHEN (CHARINDEX('PLQM' ,@test)> 0) and req.UpdatedByASPM Is Not Null THEN 1 
      ELSE 0 
     END) = 1 
相關問題