We just came across this in an old production stored proc (there is a lot more going on in here, but this is in one leg of its logic). Why would someone ever select top 0 ro
Use this to create a temporary table where the collation of your DB and TEMPDB may differ.
SELECT TOP(0) column INTO #temp FROM [mytable]
Produces a temp table with same collation as my table. This then means
SELECT * FROM #temp T INNER JOIN [mytable] M ON M.column=T.column
Does not fail due to a collation error.
By doing this, you have an empty resultset with all columns instead of no result. If the program using the stored procedure expects certain columns, it would crash otherwise.
You could use this to grab the column names.
I have mostly used it when creating temp tables from an existing DB table - without any data. The following is more reliable though than using top.
SELECT * FROM <table_name> LIMIT 0;
Let me provide an additional use:
If you want a message printed in the output rather than in messages, you could use
select top 0 0 'Your message here'
instead of
print 'Your message here'
You can use this to check if your SQL syntax is correct without loading any data.