I am attempting to write a single SQL Server script (for Reporting Services) that can run against multiple databases (Reporting Services will determine the DB to run against). The problem is that I have one table in DB that can vary from database to database and change in the number and name of columns. Here is an example of the table that could change from DB to DB.
Database 1:
- IDField
- FieldA
- FieldB
Database 2:
- IDField
- FieldX
- FieldY
- FieldX
I now want to write a script that returns the values of the arbitrary fields and coalesce them into one text value (so that Reporting Services only needs to be designed to have a single column for these fields). Here’s what I want the output to look like:
When run on Database 1:
“IDFieldValue” | “FieldA:value, FieldB:value”
When run on Database 2:
“IDFieldValue” | “FieldX:value, FieldY:value, FieldZ:value”
I know I could do this with a cursor, but that’s very resource and time intensive. I was hoping to do this with straight SQL. Any thoughts as to how?
This will be pretty close to what you’re looking for. It uses dynamic sql.
I only spent enough time to provide the concept, but I think once you look it over you’ll see where you can modify in order to format just like you want.
Hint: replace YOURTABLENAME with the actual name of the table your running against.
This script runs on a single table, but once you see the approach it wouldn’t be too much more work to modify the script to build your dynamic sql statement against many tables.