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

  • SEARCH
  • Home
  • 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 8569921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:44+00:00 2026-06-11T18:26:44+00:00

I want a program to access a table/view/stored procedure, etc. ( something materialized, let’s

  • 0

I want a program to access a table/view/stored procedure, etc. (something materialized, let’s call it X) that abstracts the real location of the data contained in three basic tables (the tables have the same definition in all locations).

I would want X to fetch the server name, catalog name and table name from somewhere (a table, probably) and access the specific three basic tables. The caller of X would not know which specific tables were being called.

How can I do this in SQL Server (2008)?

  • 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-06-11T18:26:45+00:00Added an answer on June 11, 2026 at 6:26 pm

    Like a function, a view can’t use dynamic SQL – it can’t go find some metadata reference somewhere and adjust accordingly.

    I think the closest thing to what you want is a synonym. Let’s say you have three different databases, A, B and C. In A the table you want the view to reference is dbo.foo, in B it is dbo.bar, and in Cit is dbo.splunge. So then you could create a synonym like so in each database:

    USE A;
    GO
    CREATE SYNONYM dbo.YourCommonViewName FOR dbo.foo;
    GO
    
    USE B;
    GO
    CREATE SYNONYM dbo.YourCommonViewName FOR dbo.bar;
    GO
    
    USE C;
    GO
    CREATE SYNONYM dbo.YourCommonViewName FOR dbo.splunge;
    GO
    

    Now this technically isn’t a view, but in each database you can say…

    SELECT <cols> FROM dbo.YourCommonViewName;
    

    …and it will return the data from the database-specific table.


    To do this in a stored procedure would be much simpler. Say you store the server, database and table name in some table, e.g. dbo.lookup:

    CREATE TABLE dbo.lookup
    (
      id INT PRIMARY KEY,
      [server]   SYSNAME,
      [database] SYSNAME,
      [table]    SYSNAME,
      active BIT NOT NULL DEFAULT (0)
    );
    
    -- you may want a constraint or trigger to ensure
    -- only one row can be active at any one time.
    
    INSERT dbo.lookup(id, [server], [database], [table])
      SELECT 1,N'serverA',N'databaseA',N'tableA'
      UNION ALL SELECT 2,N'serverB',N'databaseB',N'tableB';
    

    Now your program can say:

    UPDATE dbo.lookup SET active = 1 WHERE ... ?
    

    And your stored procedure can be:

    CREATE PROCEDURE dbo.whatever
    AS
    BEGIN
      SET NOCOUNT ON;
    
      DECLARE @sql NVARCHAR(MAX);
    
      SELECT @sql = N'SELECT <cols> FROM ' + QUOTENAME([server]) 
        + '.' + QUOTENAME([database]) + '.dbo.' + QUOTENAME([table])
        FROM dbo.lookup WHERE active = 1;
    
      EXEC sp_executesql @sql;
    END
    GO
    

    I still don’t understand the point, and I don’t know what you’re planning to do when two different users expect to call your program at the same time, and they each should get results from a different location.

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

Sidebar

Related Questions

I've got a legacy program, and I'm working with Access 97. I want to
So, I'm using Xcode to program with C++. I want to access the C++
I want a program that would help me create virtual devices on a virtual
I want a program that runs continually and monitors the time constantly. When it's
I want to access facebook's database. I want to have some SQL-like access, let's
I want create timer in my program so that I can cause it to
I'm trying to create a Crystal Report that reads data from an access table.
In my java program I used Hybernate technology to access MySQL database table called
I'm want to use either a hash table or a dictionary in my access
I have a VB6 program that adds a column to an MS Access database

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.