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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:09:33+00:00 2026-06-05T06:09:33+00:00

I am trying to create a dynamic query that selects a specific column based

  • 0

I am trying to create a dynamic query that selects a specific column based on an error code.

For Example I have an error table (ErrorTable) that contains multiple columns:

Transaction Number 
Transaction Amount
Transaction Date
Error Code
Error Description
+100 other columns

Errors Table

Error Code
Error Description
Error Column

I am trying to get a query that would get the following:

ErrorCode
ErrorDescription
ErrorColumn (Column Based On Error Code) 

I tried using dynamic SQL like the following and still got just the name of the column returned, maybe I am doing something wrong?

DECLARE @SQL VarChar(1000)
SELECT @SQL = 'SELECT et.ErrorCode, et.ErrorDescription, e.ErrorColumn 
               FROM ErrorTable et
                   INNER JOIN Errors e ON e.ErrorCodeID = et.ErrorCode'
Exec ( @SQL)

If I use @ErrorColumn = ‘TransactionDate’ instead of e.ErrorColumn in the dynamic query I get results, but with the query above I don’t. Any ideas?

Update 2

I get the following results with the above query:

ErrorCode    ErrorDesc              TransactionDate    TransactionAmount
1            Invalid Trans Date     TransactionDate    TransactionAmount
2            Invalid Trans Amount   TransactionDate    TransactionAmount

I want the following:

ErrorCode    ErrorDesc              TransactionDate    TransactionAmount
1            Invalid Trans Date     May 1st            
2            Invalid Trans Amount                      65 Cats
  • 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-05T06:09:34+00:00Added an answer on June 5, 2026 at 6:09 am

    Are you meaning to do this:

    DECLARE @ErrorColumn SYSNAME = N'TransactionDate';
    -- presumably the above is a parameter to the procedure
    
    DECLARE @sql NVARCHAR(MAX);
    
    SELECT @sql = N'SELECT et.ErrorCode, et.ErrorDescription, et.' 
      + QUOTENAME(@ErrorColumn)
      + ' FROM dbo.ErrorTable AS et
          INNER JOIN dbo.Errors AS e
          ON e.ErrorCodeID = et.ErrorCode;';
    
    EXEC sp_executesql @sql;
    

    Based on further information, perhaps what you want is this:

    SELECT et.ErrorCode, et.ErrorDescription, 
      TransactionDate   = CASE et.ErrorColumn WHEN N'TransactionDate' 
        THEN e.TransactionDate   ELSE NULL END,
      TransactionAmount = CASE et.ErrorColumn WHEN N'TransactionAmount' 
        THEN e.TransactionAmount ELSE NULL END
    FROM dbo.ErrorTable AS et 
    INNER JOIN dbo.Errors AS e
    ON et.ErrorCode = e.ErrorCodeID;
    

    Or this:

    SELECT et.ErrorCode, et.ErrorDescription, 
      TransactionDate   = CASE et.ErrorColumn WHEN N'TransactionDate' 
        THEN e.TransactionDate   ELSE '' END,
      TransactionAmount = CASE et.ErrorColumn WHEN N'TransactionAmount' 
        THEN e.TransactionAmount ELSE '' END
    FROM dbo.ErrorTable AS et 
    INNER JOIN dbo.Errors AS e
    ON et.ErrorCode = e.ErrorCodeID;
    

    If you need to only return the columns that actually exist in the data set, it is slightly more convoluted. You basically have to build dynamic SQL to include only the columns you have to reference in the eventual query.

    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += ',' + CHAR(13) + CHAR(10) 
      + et.ErrorColumn + ' = CASE et.ErrorColumn WHEN N''' 
      + et.ErrorColumn + ''' THEN e.' + et.ErrorColumn + ' ELSE NULL END'
    FROM dbo.ErrorTable AS et INNER JOIN dbo.Errors AS e
    ON et.ErrorCode = e.ErrorCodeID
    GROUP BY et.ErrorColumn;
    
    SELECT @sql = N'SELECT et.ErrorCode, et.ErrorDescription' 
      + @sql + '
      FROM dbo.ErrorTable AS et 
    INNER JOIN dbo.Errors AS e
    ON et.ErrorCode = e.ErrorCodeID;';
    
    PRINT @sql;
    
    -- EXEC sp_executesql @sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a linq query based on some dynamic/optional arguments passed into
I am trying to create dynamic menus from the database using the following example
I am trying to create a dynamic link that will let the user delete
I am trying to create a dynamic range using Excel functions that will sum
I am trying to create a dynamic lambda expression (parsed from text), that does
I am trying to create a dynamic hyperlink that depends on a value passed
I am trying to create a dynamic bar in HTML using javascript. I have
I'm trying to create a generic list from a specific Type that is retrieved
I'm trying to create a query to rank products based on onsite browsing habits
I'm trying to create a dynamic report in Excel. I have lots of sales

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.