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
Are you meaning to do this:
Based on further information, perhaps what you want is this:
Or this:
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.