简体   繁体   中英

display passed parameter in an ssrs report even if there are no associated records in the database

I need to create an SSRS report that is passed a list of parameters and return all the parameters, even if there are no records associated with the parameter. I have posted below of what I am trying to accomplish.

Passed parameters: 12388501, 1238853, 1238858, 123885900, 12388573

And would like the final report to look like the example below: 在此处输入图片说明

The parameters passed in this example are Account Numbers. How can I get the Account Number to display as a record even though it is not contained in the database?

I am using SQL Server 2012 database, SSMS for development of the query and will ultimately create the report in SSRS. I hope my wording of this question makes sense. If there is anything missing in my query please let me know and I will provide it. Thanks in advance!

Step 1: Split your string into rows of a table. My understanding is that Erland Sommarskog is regarded as an authority on handling lists in SQL Server: Arrays and Lists in SQL Server 2008 Using Table-Valued Parameters (revised in 2012. He has a page devoted to the broader topic/earlier SQL Server versions at Arrays and Lists in SQL Server .) There are other methods for splitting strings into table-valued parameters via user-defined functions, etc. that may perform reasonably well for small lists similar to what you have here.

Step 2: using that table-valued parameter that you've populated from the splitting of the string parameter, form your query like the one below. The INNER JOIN in the first SELECT will give you only those records that match an AccountNumber in your inline table and the LEFT OUTER JOIN with the WHERE clause in the second SELECT will give you only AccountNumber values that don't exist in myTable. You can substitute other values for the NULL values I've stubbed in as long as they match the data types of the corresponding fields in myTable.

SELECT mt.AccountNumber,mt.LastName,mt.FirstName,mt.ContactNumber,mt.Address,mt.City,mt.State,mt.Zip 
FROM myTable mt JOIN @t t ON mt.AccountNumber = t.AccountNumber
UNION
SELECT t.AccountNumber,NULL LastName,NULL FirstName,NULL ContactNumber,NULL Address,NULL City,NULL State, NULL Zip 
FROM @t t
LEFT OUTER JOIN myTable mt ON t.AccountNumber = mt.AccountNumber 
WHERE mt.AccountNumber IS NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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