简体   繁体   English

在SSRS数据集SQL查询中创建新行以在报告参数中使用

[英]Creating a new row in SSRS dataset SQL query to use in a report parameter

I am going round in circles with a bit of SQL and would appreciate some help. 我会用一些SQL来回转转,希望对您有所帮助。

I've looked up creating temp tables, nested Select statements (where advice seems to be to avoid these like the plague) and various uses of Case statements but I can't seem to find a solution that works. 我查找了创建临时表,嵌套的Select语句(建议尽量避免类似瘟疫的用法)以及Case语句的各种用法,但我似乎找不到有效的解决方案。 I'd say I'm beginner level for SQL. 我想说我是SQL的初学者。

I have a table with 10 relevant records. 我有一张桌子,上面有10条相关记录。 The query that works to return all the relevant entries in the table is: 返回表中所有相关条目的查询为:

SELECT
     TblServAct.ServActId   
    ,TblServAct.ServActName  
FROM TblServAct 
WHERE TblServAct.ServActExclude IS NULL 
ORDER BY TblServAct.ServActName

Here is where I run into problems: 这是我遇到问题的地方:

When the parameter (@YESNOActivity) = Yes, I want all the rows in the table to be returned. 当参数(@YESNOActivity)=是时,我希望返回表中的所有行。 I have managed to do this with a CASE statement 我已经用CASE语句做到了这一点

...however when the parameter (@YESNOActivity) = No, I want ONLY ONE row to be returned which doesn't actually exist in the table (and should not be inserted into the actual table). ...但是,当参数(@YESNOActivity)=否时,我只希望返回表中实际上不存在的一行(并且不应将其插入到实际表中)。 The values that I need to insert are: ServActId = 101 and ServActName = 'Select YES in Parameter2 to filter by Service Activity' 我需要插入的值是:ServActId = 101和ServActName ='在Parameter2中选择YES以按服务活动进行过滤'

For background, the reason I am doing this is because I have found SSRS report parameters to be especially difficult to conditionally format. 出于背景原因,我这样做的原因是因为我发现SSRS报告参数特别难以按条件格式化。 I want to use the dataset above to return a message in a parameter (lets call it parameter2) that the user needs to select yes in (@YESNOActivity) in order to see the full selection list in parameter2. 我想使用上面的数据集在参数中返回一条消息(叫它为parameter2),用户需要在(@YESNOActivity)中选择是,以便在parameter2中看到完整的选择列表。

If I can get this to work I can see lots of potential for re-use so all advice appreciated 如果我可以使用它,我可以看到很多潜在的重用潜力,因此所有建议都值得赞赏

Thanks Eileen 谢谢艾琳

I believe this should do the job, just include your parameter in the WHERE clause and UNION it with your own row of data. 我相信这应该做的工作,只是包括在参数WHERE子句和UNION它用自己的一行数据。

SELECT
     TblServAct.ServActId   
    ,TblServAct.ServActName  
FROM TblServAct 
WHERE TblServAct.ServActExclude IS NULL
  AND @YESNOActivity = 'Yes'
UNION ALL
SELECT
     ServActId = 101
    ,ServActName = 'Select YES in Parameter2 to filter by Service Activity'
WHERE @YESNOActivity = 'No'
ORDER BY TblServAct.ServActName

One way is to use this query: 一种方法是使用此查询:

SELECT 
  TblServAct.ServActId   
  ,TblServAct.ServActName  
FROM TblServAct 
WHERE TblServAct.ServActExclude IS NULL 
  AND 'Yes' = @YESNOActivity
UNION ALL
SELECT 
  101 AS ServActId 
  ,'Select YES in Parameter2 to filter by Service Activity' AS ServActName
WHERE 'No' = @YESNOActivity
ORDER BY TblServAct.ServActName

Another way would be to create two data flows and use your variable in a constraint to send the processing to one or the other. 另一种方法是创建两个数据流,并在约束中使用变量将处理发送给一个或另一个。

A third way would be to put an expression on the SQL command and use your variable to switch between two SQL statements. 第三种方法是在SQL命令上放置一个表达式,然后使用变量在两个SQL语句之间切换。

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

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