Dynamic Database Selection in SQL/SSRS

Phill Devey 0 Reputation points
2023-06-15T15:47:27.3233333+00:00

I work in an organization that has 50+ databases with identical schemas but data for different divisions and legal entities of the business. Some of the SSRS reports pull data from multiple databases into a single report however, the list of databases needed in a report can change several times a year requiring the Datasets in the reports to be rebuilt to target different databases,

This scenario creates a requirement to dynamically specify the databases needed for the query however, this is not something that SQL or SSRS provides direct support for and requires the use of Dynamic SQL to enable. As SQL is a Database engine, it's somewhat ironic that it does not have the native capability to dynamically specify the database a query should be run against and, that over the many versions of SQL, this capability has never been introduced into SQL or SSRS.

The functionality can be achieved by the use of Dynamic SQL but, Dynamic SQL has many complications associated with it and is not a great option when writing queries inside SSRS.

I have been posting this question on other forums but does anyone know of native ways this functionality can be achieved? Every solution I have found requires some kind of workaround or multi-step process to implement.

Thanks

SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,566 Reputation points
    2023-06-19T02:27:39.7033333+00:00

    Hi @Phill Devey

    Every solution I have found requires some kind of workaround or multi-step process to implement.

    Not an expert in SSRS, but from dynamic SQL side, I didn't see simple method to implement.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Phill Devey 0 Reputation points
    2023-06-19T13:44:14.8666667+00:00

    Thank you for ever ones input. I have worked with SQL for almost 20 years so I knew that natively, SQL has poor support for dynamically query data from multiple databases. I was however hopeful that someone would know a clean & efficient way to handle this in SSRS (it is after all; a reporting tool).

    The solution I detailed above (calling an SP from within the SSRS Query) seems to be the best solution I can find to fit the needs of my scenario, where the main query is NOT written as dynamic SQL. With that said, selecting data from multiple databases is a legitimate need for SQL, it may not be required in all scenarios but, it IS a legitimate requirement. Perhaps this is something MS need to consider as part of SQL's future design or, if not SQL, then it's own reporting tool. Hardcoding Database names into SQL queries is not a sustainable practice as the more you do it, the more resources are required to maintain the reports. In a larger organization, those kinds of limitations and requirements result in your products being replaces by other technologies that are more efficient to work with.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.