SSRS:在 sql where 子句中使用存在条件,同时从 SSRS 参数传递值

SSRS: Using exists condition in sql where clause while passing values from SSRS Parameter

Table : incident
----------------
incident_id   usr_id    item_id   Inc_Date
10059926       191       61006    8-22-2015
10054444       222        3232    6-7-2015

Table: act_reg
--------------
 act_reg_id  act_type_id  incident_id    usr_id  act_type_sc
 454244         1        10059926         191    ASSIGN
 471938        115       10059926         191    TRAVEL TIME
 473379        40        10059926         191    FOLLOW UP
 477652        115       10059926         191    TRAVEL TIME
 489091        504       10059926         191    ADD_ATTCHMNTS
 477653        504       10054444         222    ADD_ATTCHMNTS

参数:@attach(值=1,标签=是&值=0,标签=否)

 Result (While I am selecting 'Yes' in dropdown)
 ----------------------------------------------
 incident_id   usr_id    item_id  
 10059926       191      61006    
 10054444       222       3232

我的查询:

SELECT  incident.incident_id,incident.usr_id,incident.item_id
FROM  incident 
where exists (select * from act_reg 
              where incident.incident_id = act_reg.incident_id
                  and act_reg.act_type_sc (case when @attach=1 and act_reg.act_type_sc='ADD_ATTCHMNTS' then NULL else act_reg.act_type_sc end  )
           )

请帮我解决这个问题。

这是在 SQL Fiddle 中使用您的数据的查询。我用 0.

替换了参数

http://www.sqlfiddle.com/#!6/7f8ef/3

SELECT  incident.incident_id,incident.usr_id,incident.item_id
FROM  incident 
WHERE EXISTS (SELECT * from act_reg 
              WHERE incident.incident_id = act_reg.incident_id
 AND (act_reg.act_type_sc <> 'ADD_ATTCHMNTS' OR @attach = 1)
           )

您应该将查询更改为以下内容:-

SELECT  incident.incident_id,incident.usr_id,incident.item_id
FROM  incident
left join act_reg 
on incident.incident_id = act_reg.incident_id and act_reg.act_type_sc = 'ADD_ATTCHMNTS'
where 
((@attach = 1 and act_reg.act_reg_id is not null) or 
(@attach = 0 and act_reg.act_reg_id is null))
group by incident.incident_id,incident.usr_id,incident.item_id

SQL Fiddle