Turning a multi-value parameter into a temp table in SQL Server Business Intelligence Development Studio

帅比萌擦擦* 提交于 2020-05-15 10:56:05

问题


I want to create a report in MS SQL Server BIDS (SSMS and Visual Studio). The user would enter a list of email addresses as a parameter. So @pEmails would be 'foo@bluh.com', 'bar@meh.org', etc. These email addresses may or may not be in a table.

I can simply do:

and Table.Email in (@pEmails)

and that works, except I need to return the email address if it's NOT found as well. So the results would be something like:

|email       |found in table|  
|------------|--------------|  
|foo@bluh.com|  Y           |  
|bar@meh.org |  N           |

I was thinking I could take the list of values entered as the @pEmails parameter and create a temp table with them, which I could then left join with, but my attempts to do so have not worked out.

declare @pEmails table (EmailAddress varchar(255));
insert into @pEmails values (@ReportParameter1);

select
*
from
@pEmails

The above works if only a single value is put into @ReportParameter1, but not if multiples are in it.

I am using SQL Server 2008. Any suggestions on how best to proceed?


回答1:


As has been stated, you need some kind of split function, for analysis on the performance of various methods Split strings the right way – or the next best way is an excellent read. Once you have your function, you then need to define your query parameter as a string, rather than a table:

So your query would actually become:

DECLARE @pEmails TABLE (EmailAddress varchar(255));

INSERT @pEmails (EmailAddress)
SELECT  Value
FROM    dbo.Split(@pEmallString);

Then go to your dataset properties, and instead of passing the multivalue parameter @pEmails to the dataset, instead create a new one @pEmailString, and set the value as an expression, which should be:

=Join(Parameters!pEmails.Value, ",")

This turns your multivalue parameter into a single comma delimited string. It seems pretty backwards that you need to convert it to a delimited string, only to then split it in SQL, unfortunately I don't know of a better way.




回答2:


Here are some learnings on this topic (standing on the shoulders of the information elsewhere in this thread).

Set a parameter (select 'multiple values' checkbox):

InputList

Establish dataset query:

SELECT * 
INTO #InputTemp 
FROM  STRING_SPLIT(@InputListJoin, ',')
SELECT LTRIM (value) AS ValueName FROM #InputTemp

Establish dataset parameters:

Name: @InputList  |  Value: [@InputList]
Name: @InputListJoin  |  Value(expression): =Join(Parameters!InputList.Value,", ")

The element names can be changed as needed.

You can now join #InputTemp to another table to find unmatched entries.

Somewhat on topic, other details that might be helpful:

[@InputList.IsMultiValue] --> true/false whether your parameter is multi-value (not whether there are multiple values)
[@InputList.Count] --> count of items in input list (excludes blank lines)
=Parameters!InputList.Value(2) --> return third value from list (counting from zero)


来源:https://stackoverflow.com/questions/40534211/turning-a-multi-value-parameter-into-a-temp-table-in-sql-server-business-intelli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!