1

I'm trying to use a dynamic range of columns fetched from another query, specifically I want the results of this query:

SELECT COLUMN_NAME as DYNAMIC_COLUMN FROM information_schema.columns 
WHERE table_name='TheTableName' AND column_name like 'TheColumnName%'

As columns for the SELECT statement of this query:

SELECT TheColumnResults
FROM AnotherTable

My (wrong) approach is something like this:

SELECT(
SELECT COLUMN_NAME as DYNAMIC_COLUMN FROM information_schema.columns 
WHERE table_name='TheTableName' AND column_name like 'TheColumnName%'
) AS TheColumnResults
SELECT TheColumnResults
FROM AnotherTable

I just need a guidance on how to approach this or even if it's possible to set a dynamic range of columns at the SELECT statement.

Other (failed) approach I've used:

SELECT *
FROM DATABASE.AnotherTable
INNER JOIN information_schema.columns 
WHERE Exists (SELECT COLUMN_NAME AS DYNAMIC_COLUMN FROM
information_schema.columns 
WHERE table_name='TheTableName' AND column_name like 'TheColumnName%' AND TABLE_SCHEMA = 'DATABASE')

Browse other questions tagged or ask your own question.