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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:15:52+00:00 2026-05-17T20:15:52+00:00

I have a bit of an odd database model for a project – I’m

  • 0

I have a bit of an odd database model for a project – I’m going to have several tables with the same definition. Each table stores information for different clients and because of security restrictions, we cannot put the data together in one table. So it would look something like this…


table ClientOneData
(
    Id (PK, int, not null),
    Col1 (varchar(50), not null),
    etc.
)

table ClientTwoData
(
    Id (PK, int, not null),
    Col1 (varchar(50), not null),
    etc.
)

I want a single stored procedure to retrieve data from the appropriate table. I could do that by just passing the table name as a param to the proc and then building up a string of sql to execute…


CREATE PROCEDURE GetData
    @TableName varchar(100)

AS
BEGIN

    DECLARE @sql varchar(max)
    SET @sql = 'SELECT * FROM ' + @sql

    exec(@sql)

… but that seems fundamentally wrong to me. And replicating code, either in the form of a giant case statement or by “one-offing” the proc and creating a new one for each client, also seems wrong.

Is there a better way to do this?

I’m pretty open to suggestions, anything from something I can do in the proc to re-working the data model (short of dumping all the data into a single table). Upgrading to SQL-Server 2008 or 2010 might be an option, but would be a last resort.

  • 1 1 Answer
  • 2 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-17T20:15:53+00:00Added an answer on May 17, 2026 at 8:15 pm

    If you have a reasonably small number of tables you could combine them into a view and query that. Example:

    create view vw_MyTables
    AS
       SELECT 'table1' as tableName, * from table1
       UNION
       SELECT 'table2', * from table2 
       UNION
       SELECT 'table3', * from table3 
    
    
    SELECT * FROM vw_MyTables
    WHERE tableName = @TableName
    

    Otherwise I think your only option is dynamic sql…

    If you are willing to change your schema around that would probably be best. Is there a reason you store identical(is it identical?) information for clients in different tables?

    I have used the following structure to represent this type of data before:

    Client
    --------
    ClientId - pk
    ClientName
    
    
    ClientData
    -----------
    DataId - pk
    ClientId - fk to Client
    Whatever
    

    This is basically what my view does but if you do it this way you can create indexes on it and get your datas wicked fast.

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

Sidebar

Related Questions

I have a database table that includes a two bit fields: IsEvenSide and IsOddSide
I have a bit of an odd scenario where I have a table that
I have what seems to me as a bit of an odd scenario, well
This is a bit of an odd situation. Here's what's happening. So, we have
I have a bit of a unique problem with an upcoming project; I need
I have a bit of an odd issue. I have a projects controller where
I know it sounds a bit odd but I have a string: <img src=image1.gif
I have a bit of an odd situation, and I'm struggling with populating a
We have a bit of an odd setup, which comes from the fact we
I have a bit of an odd problem. I have been populating a UITextView

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.