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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:06:18+00:00 2026-06-12T08:06:18+00:00

Say I have a table Example with three columns: X, TableName, ColumnName . TableName

  • 0

Say I have a table Example with three columns: X, TableName, ColumnName.

TableName stores the name of a table in the database, and ColumnName stores the name of the column that I want to join X to. The problem is that different rows need to be joined to different tables (in a well defined way).

I want to be able to do something like:

Select *
from Example join TableName on X = ColumnName

Which is obviously not going to work.

I’m pretty new and did some googling and it seems like I might be able to use a cursor to loop over the rows and use dynamic SQL since the names of the Tables and Columns are stored in the database, but I’ve heard that both Cursors and Dynamic SQL should be avoided, so I haven’t looked into the details of that. I was hoping there was a prefered way to solve this issue.

Thanks.

  • 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-12T08:06:19+00:00Added an answer on June 12, 2026 at 8:06 am

    Dynamic SQL can be slower than a stored proc, but not always, and has gotten much better as SQL Server has matured. Whenever possible, non-dynamic SQL should be used as it is cleaner and easier to maintain. However, dynamic SQL does have its place (ORM tools like Entity Framework and NHibernate use dynamic sql almost exclusively). Cursors should be avoided if at all possible, but SQL does support a WHILE loop that can be used instead.

    DECLARE @Identity int = SELECT MIN(ex.Identity) FROM Example ex
    DECLARE @TableName varchar(200)
    DECLARE @ColumnName varchar(200)
    WHILE @Identity IS NOT NULL BEGIN
        SELECT
            @TableName = ex.TableName,
            @ColumnName = ex.ColumnName
        FROM
            Example ex
        WHERE
            ex.Identity = @Identity
    
        /*
            build and execute your dynamic sql here
        */
        SET @Identity int = SELECT MIN(ex.Identity) FROM Example ex WHERE ex.Identity > @Identity
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so lets say I have a table comprising three columns. The first column
For example, say I have a table with 10 columns in it, but I
I have an html table with say 5 columns. Three of the columns I
I have a table that has two columns, ID and Value. For this example
Say I have a table that has the following columns: AutoID (PK, int not
This question can best be asked by example. Say I have a database table
Lets say I have a table with three columns, key , value and priority
Assume that I have one big table with three columns: user_name, user_property, value_of_property. Lat's
Suppose that I have a table with three columns: EventID (PK) TagName TagValue I
First time post. As an example, say I have a table with 12 entries;

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.