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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:22:54+00:00 2026-05-15T15:22:54+00:00

I have two tables A and B, with dynamic columns where I have no

  • 0

I have two tables A and B, with dynamic columns where I have no idea what columns are key inside them, except from another table called C.

The C table specifies which column/s is a key column in tables A and B. There can be 1 or more key columns.

My question is, how would I generate such a query where I select all rows from A where the key columns are equal to the same key columns in B?

One idea I had was to create a text query that I execute with sp_executesql, but I need some good ideas on how to generate the query.

First of all, I would select all key columns from table C for the table A and B to my declared table @keyColumns.

Then I would use a while loop to go through all key columns inside @keyColumns and generate the query and execute it with sp_executesql.

For example:

UPDATE A 
SET ... 
FROM B INNER JOIN A 
ON A.keycol1 = B.keycol1 AND A.keycol2 = B.keycol2 AND ...

Just to make it clear, the C table only specifies key columns for the table B, and from that I know A has the same key columns.

But I want to know if there’s a better way to solve this.

  • 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-15T15:22:55+00:00Added an answer on May 15, 2026 at 3:22 pm

    Are the key columns held in ‘C’ the primary key? If so you can retrieve these from INFORMATION_SCHEMA.TABLE_CONSTRAINTS, and INFORMATION_SCHEMA.KEY_COLUMN_USAGE as described here rather than using a different table.

    You have to use dynamic SQL for this I think. There is no syntax like FROM B JOIN A ON PRIMARY KEYS. Instead of the WHILE loop though you can just concatenate your query through a SELECT as below.

    DECLARE @DynSql nvarchar(max)
    DECLARE @TableA sysname
    DECLARE @TableB sysname
    
    SET @TableA = 'A'
    SET @TableB = 'B';
    
    WITH C AS
    (
    SELECT 'B' AS [Table], 'keycol2' As col UNION ALL
    SELECT 'B' AS [Table], 'keycol1' As col UNION ALL
    SELECT 'X' AS [Table], 'keycol1' As col
    )
    
    SELECT @DynSql = ISNULL(@DynSql + ' AND ','')+ @TableA + '.'+QUOTENAME(col) + '= ' + @TableB + '.'+QUOTENAME(col)
    FROM C WHERE [Table] = @TableB
    
    IF @@ROWCOUNT=0
    RAISERROR('No Entry found for table %s',16,1,@TableB)
    
    SET @DynSql = 
    'UPDATE ' + @TableA + ' 
    SET ... 
    FROM ' + @TableB + '  INNER JOIN ' + @TableA + ' ON  
    ' + @DynSql
    
    PRINT @DynSql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database, called ratings and movies . Ratings: |
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
I have two dynamic tables (tabx and taby) which are created and maintained through
I have two tables; Leads, Territories and Referrers. Lead has columns: ID, Name, TerritoryId
I have two Data Tables and these are completely dynamic. These would be generated
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables, both with start time and end time fields. I need

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.