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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:10:38+00:00 2026-05-29T11:10:38+00:00

I have two very similar tables in our database, and I need to write

  • 0

I have two very similar tables in our database, and I need to write a stored procedure for my Visual Studio 2010 Web Application to read the data from one of these tables given a table number.

Currently, we only have two tables to select from, but I can see this growing to more as this project grows.

This is sort of what I am trying to do, but this code is not correct:

PROCEDURE [dbo].[spGetData]
  @tableID int
AS
BEGIN
SET NOCOUNT ON;
    declare @col1 nvarchar(50), @table nvarchar(50)
    set @col1=case when @tableID=1 then 'SMRequestID' else 'WHRequestID' end
    set @table=case when @tableID=1 then 'SMRequest' else 'WHRequest' end

    select @col1 as 'Request', WorkOrder, PartNumber, Qty, EmployeeID
    from @table
END

Basically, the ColumnName and TableName depend on the @tableID parameter that will be passed in.

How would I go about doing that?

Note: My searches are not turning up anything related, but I am a C# developer and not a database developer. I imagine this has been asked before, it is just I am not using the right keywords.

  • 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-29T11:10:38+00:00Added an answer on May 29, 2026 at 11:10 am

    One option would be to use a conditional based upon the ID and put the code for a specific table in each section for the table.

    I prefer this method to get away from the dynamic sql and allow the database server to get a fighting chance to optimize the thing for speed reasons by precompiling.

    NOTE: database servers are pretty bad at string manipulation (create dynamic sql) in general.

    EDIT1: EXAMPLE

    FOR INSTANCE: THIS SQL

    declare @mytest varchar(5)
    set @mytest = 'PROCS'
    
    IF @mytest = 'PROCS'
    BEGIN /* STORED PROCS */
      SELECT DISTINCT 
        o.name AS ObjectName_StoredProcedure
      FROM sysobjects as o 
      WHERE o.xtype = 'P'
    END
    ELSE
    IF @mytest = 'DEFAULT'
    BEGIN
       SELECT DISTINCT 
        o.name AS ObjectName_StoredProcedure
      FROM sysobjects as o 
      WHERE o.xtype = 'D'
    END
    

    gives you the store procedure names or the default constraints depending on what you pass to the parameter.

    EDIT2: Based on OP code:

    CREATE PROCEDURE [dbo].[spGetData]   
      (@tableID int  )
       AS 
      BEGIN
         SET NOCOUNT ON;
         IF @tableID = 1
         BEGIN
           SELECT SMSRequestId AS 'Request',
              WorkOrder, PartNumber, Qty, EmployeeID 
           FROM SMRequest
         END   
         IF @tableID = 2
         BEGIN
           SELECT WHRequestID AS 'Request',
              WorkOrder, PartNumber, Qty, EmployeeID 
           FROM WHRequest
        END
     END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two very similar programs each trying to run two threads OddThread and
I have two very similar specs for two very similar controller actions: VoteUp(int id)
I have two functions that are very similar and i'd like to make a
I have a script in PHP which retrieves two very similar files and performs
I have an issue where I have two pages that deal with very similar
i have two tables in my database one is A other one is B
I have a database which has very similar rows within the same table. Those
I have two very similar pieces of ASP.NET code that send a file in
The database I am working with currently does have two tables, one holding the
I have two databases Database 1: (Allows users to add posts similar to a

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.