Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6167321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:29:30+00:00 2026-05-23T22:29:30+00:00

I am attempting to write a single SQL Server script (for Reporting Services) that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:29:31+00:00Added an answer on May 23, 2026 at 10:29 pm

    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.

    declare @pQuery varchar(max)
    declare @pFields varchar(max)
    
    SELECT @pFields = COALESCE(@pFields + '''' + '  ' +column_name + ': ''+ ', '') + 'Cast(' + column_name + ' AS VarChar(max)) + ' 
    FROM information_schema.columns WHERE table_name = 'YOURTABLENAME';
    
    SELECT @pQuery = 'SELECT ' + @pFields + '''' + '''' + ' AS [ReportRow] FROM YOURTABLENAME';
    
    SELECT @pQuery  /** just to see what you've created */
    
    exec(@pQuery);  /** execute dynamic sql and get back concatenated rows */
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to write a one-line Perl script that will toggle a line
I'm attempting to write a performance testing function that can take any function, run
I'm attempting to write a script for myself that will log in to a
I am attempting to write some code that will expediently process video frames. I
I'm attempting to write a wrapper so that my C# application can use a
I'm attempting to write a CompiledQuery using Linq-to-Entities that will replace a stored procedure
I am attempting to write an ASP.net web service that will be utilized by
I'm attempting to design a system that will allow the processing of multiple types
I am attempting to write a JS function (using prototype in rails) that will
I'm attempting to write a query that will allow me to get any record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.