I need to create a dynamic sql query in order to return all values in the first column of a table. The table's name needs to be a variable @tableName ( i will run this query for multiple tables depending on several conditions). Also, i don't know exactly the first column's name but it is formed out of Id_@tableName .
I need something like below but written dinamically:
select
(select column_name from INFORMATION_SCHEMA.columns where table_Name= @tableName and ordinal_position=1)
from @tableName
Could you please help me? Thank you in advance!