I am looking at some code and came across something extremely unfamiliar to me ; Google did not prove fruitful for results so I was wondering if anyone could explain what the following code does? It does not refer to any of my tables or databases, so I assume it’s general code and I needn’t provide my database layout? Thanks very much.
The code :
SELECT ROW_NUMBER() OVER (ORDER BY Object_ID) AS weeks
FROM SYS.OBJECTS
It will select the numbers from 1 to N where N is the number of rows in sys.objects. It will not guarantee a sort-order.
Probably this code is intended to provide all week numbers (omg!) under the assumption that there are at least 52 rows in sys.objects.
This code, however, will return more than 52 rows and the result is not guaranteed to be ordered. I recommend you get rid of this nastiness.
Edit: As an alternative, I’d choose to create the following table:
CREATE TABLE Weeks (WeekNumber TINYINT NOT NULL PrimaryKey)and fill it appropriately. This will be even faster than selecting from sys.objects because this custom table will be smaller and correctly sorted.