What does a SELECT statement do?
I am looking at some code and came across something extremely unfamiliar to me; Google hasn't turned out to be fruitful for the results, so I was wondering if someone could explain what the following code does? It doesn't apply to any of my tables or databases, so I'm assuming it's generic code and I don't need to provide my database layout? Thank you so much.
Code:
SELECT ROW_NUMBER() OVER (ORDER BY Object_ID) AS weeks
FROM SYS.OBJECTS
source to share
It will select numbers from 1 to N, where N is the number of lines in sys.objects. This does not guarantee the sort order.
This code is probably meant to provide all week numbers (omg!) Assuming there are at least 52 lines in sys.objects.
This code, however, will return more than 52 lines and the result is not guaranteed. I recommend that you get rid of this nasty thing.
Edit: Alternatively, I would select the following table: CREATE TABLE Weeks (WeekNumber TINYINT NOT NULL PrimaryKey)
and fill it in correctly. This will be even faster than selecting from sys.object because this user table will be smaller and sorted correctly.
source to share
The developer uses the records in the system table sysobjects
to get a list of sequential numbers using the window function ROW_NUMBER()
and flatten the column as "Weeks". The number of rows in the view sys.objects
will change based on the objects defined in the database, so why would someone do this outside of me ...
If you need a simple list of sequential numbers, there are more predictable ways to get them.
source to share