I'm using SQL Server 2012. I would like to write a query where first I look for a column name in specific table using LIKE "%MyName" phrase and then use this column name in select statement to find a value in that column in the table Prąd.
I've tried to do so, using tips from this topic. The code I wrote so far looks like this:
SELECT 
    (
    SELECT c.name AS ColName
    FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
    WHERE c.name LIKE 'energia%'
    )   
FROM Prąd
WHERE ID = 
    (
    SELECT MAX(ID) 
    FROM 
    Prąd
    )
What I get as a result is just the column name, not a value in specified column and row in the table Prąd.
 
     
     
    