简体   繁体   English

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

[英]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

Parameter: @attach (value=1, Label=Yes & Value=0, Label=No)参数:@attach (value=1, Label=Yes & Value=0, Label=No)

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

My Query:我的查询:

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  )
           )

Please help me on this.请帮我解决这个问题。

You should change your query to the following :-您应该将查询更改为以下内容:-

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 SQL小提琴

Here's the query that's working in SQL Fiddle with your data.这是在SQL Fiddle 中处理您的数据的查询。 I substituted the parameter with a 0.我将参数替换为 0。

http://www.sqlfiddle.com/#!6/7f8ef/3 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)
           )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM