This is a bit of an XY answer, but if you don't mind hard coding for column names, I suggest you do just that and avoid dynamic SQL - and the loop - completely. Dynamic SQL, generally considered the last, opens up security problems (SQL injection attacks) if they are not careful, and can often be slower if queries and execution plans cannot be cached.
If you have a ton of column names, you can write a quick code snippet or merge in Word to make a replacement for you.
However, how to get the column names, assuming it is SQL Server, you can use the following query:
SELECT c.name FROM sys.columns c WHERE c.object_id = OBJECT_ID('dbo.test')
Therefore, you can build your dynamic SQL from this query:
SELECT 'select ' + QUOTENAME(c.name) + ',count(*) from [BT].[dbo].[test] group by ' + QUOTENAME(c.name) + 'order by 2 desc' FROM sys.columns c WHERE c.object_id = OBJECT_ID('dbo.test')
and loop with the cursor.
Or compile it all together in one batch and execute. Here we use the FOR XML PATH('') trick:
DECLARE @sql VARCHAR(MAX) = ( SELECT ' select '
Note. I use the built-in QUOTENAME function to avoid column names that require escaping.
lc.
source share