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 6761687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:13:18+00:00 2026-05-26T14:13:18+00:00

I need to create a view or a table valued function that returns one

  • 0

I need to create a view or a table valued function that returns one result set from a query ran against a dynamic list of databases (stored in a table). All of the databases have the same structure, and the view/tvf should contain the equivalent of a union all of the query for each database in the databases_table. The big-picture of all of this is that I need the results of this query to be available via ODBC.

After a lot of looking online, I’ve come up with a handful of potential solutions, just none that perfectly fit what I’m trying to accomplish.

The first thing that I looked into was having a view with a select for each distinct database, all unioned together. This worked perfectly, except for the fact that it is as far away from being maintainable as I can possibly imagine. I will have 25-100 of these sorts of multi-database queries, all of which have to be ran against a constantly changing set of databases.

I am still looking at possibly using this method, and making a stored procedures to dynamically create the views for me, but this is still not very useful, as I have been asked to make this entire process hands-free. Doing it this way would require someone to trigger the view update before we attempt to connect to the views via ODBC. I would like to avoid this if at all possible.

The second thing that I looked into got me a lot closer to what I needed. I made use of the sp_msforeachdb stored procedure, checked to see if the database was on the list, and if so I added the results of the query to a table variable. This works just fine, but the issue was that the dynamic sql is making it impossible for me to wrap it into a table valued function or view…

Declare @RETURNTABLE Table(variable1 varchar(20), variable 2 varchar(30))  
INSERT INTO @RETURNTABLE 
exec sp_msforeachdb 'IF "?" IN (SELECT DatabaseName FROM DatabaseList) BEGIN     SELECT "?" [DatabaseName], variable1 , variable2 from [?].dbo.myTable END'  
SELECT * FROM @DBINFO

This works perfectly when running it from the query from ms ssms, but as I’ve said, putting it in a veiw or tvf has proven beyond my ability. As far as I am aware, the dynamic SQL precludes the use of tvf’s and views aren’t able to work with variables.

Is there some way that I can make the results of the above code available via ODBC. Is there an alternative solution that I’m missing?

  • 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-26T14:13:19+00:00Added an answer on May 26, 2026 at 2:13 pm

    As far as I can tell a stored proc using dynamic SQL is your only choice.

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

Sidebar

Related Questions

following background: i have a Table-valued Function that returns a Table that is a
The situation is, that i need to create table in grid view looking like
I need to create a view from several tables. One of the columns in
I understand that we need to create MXML file to define a view. Suppose
I have a table with about 2,000,000 rows. I need to query one of
I have been thinking about a complicated view/query I need to create but I
In my app, I create view controller objects as I need them. When a
I need to create a form with, half linear view (textboxes and dropdownlists in
UPDATED: I am using MySQL statement to create a view: I need to show
Need to create a custom DNS name server using C which will check against

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.