SSRS:在从SSRS参数传递值时,在SQLWHERE子句中使用exists条件
参数:@attach value=1,Label=Yes&value=0,Label=NoSSRS:在从SSRS参数传递值时,在SQLWHERE子句中使用exists条件,sql,sql-server-2008,reporting-services,ssrs-2008,ssrs-2008-r2,Sql,Sql Server 2008,Reporting Services,Ssrs 2008,Ssrs 2008 R2,参数:@attach value=1,Label=Yes&value=0,Label=No 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_
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
我的问题是:
Result (While I am selecting 'Yes' in dropdown)
----------------------------------------------
incident_id usr_id item_id
10059926 191 61006
10054444 222 3232
请在这方面帮助我。以下是使用SQL处理数据的查询。我用0替换了参数
您应该将查询更改为以下内容:-
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)
)
有什么问题吗?你有错误吗?您是否缺少act_reg.act_type_sc和case之间的=号?@HannoverFist:Alter adding=我也没有得到确切的结果。选择“否”时的预期结果集?我认为您的问题在于空值。WHERE子句将始终获取所有记录,因为act_type_sc将始终等于NULL。如果要在@attach=0时排除ADD_ATTCHMNTS,请尝试使用reg.act_type_sc'ADD_ATTCHMNTS'或@attach=1,而不是当前的和行。@BhupeshC:当我选择“否”时,当@attach值为0时,我需要获得不带任何附件信息的结果数据。结果会怎样?在这里,它显示所有。但是它不应该显示10059926和10054444,它只显示一个答案:incident_id usr_id item_id 10059926 191 61006尝试在我设置的SQL FIddle演示中运行它。使用0,当@attach为1时,它得到一条记录和两条记录。是的,我尝试过,当它为0时,结果显示10059926,它应该没有值,我认为当@attach=1时它应该是两条记录,当它等于0时它应该是一条记录。act_reg表格中有一个事件10059926,其act_类型的sc不是ADD_附件。这不是你的查询应该做的吗?
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