Fix “Invalid object name ‘GENERATE_SERIES'” in SQL Server

If you’re getting SQL Server error 208 that reads “Invalid object name ‘GENERATE_SERIES’“, it could be that you’re calling GENERATE_SERIES() in a version of SQL Server that doesn’t support this function.

The GENERATE_SERIES() function was introduced in SQL Server 2022 (16.x), and so if we try to call it in an earlier version of SQL Server, we’ll get the above error.

So if you’re running this on an earlier version, you’ll need to upgrade before you can run it successfully. You’ll also need to run it on a database with a compatibility level of at least 160.

Example of Error

Here’s an example of code that produces the error:

SELECT value FROM GENERATE_SERIES( 1, 5 );

Result:

Msg 208, Level 16, State 1, Line 1
Invalid object name 'GENERATE_SERIES'.

I ran that code in an earlier version of SQL Server (actually I ran it in Azure SQL Edge Developer (RTM) – 15.0.2000.1559).

The error tells me that GENERATE_SERIES() is not a valid object name.

I got the error because my version of SQL Server doesn’t support the GENERATE_SERIES() function.

Solution

To fix the above issue, we need to run the code on SQL Server 2022 or greater, against a database with a compatibility level of at least 160.

Here’s what it looks like running the code in SQL Server 2022:

SELECT value FROM GENERATE_SERIES( 1, 5 );

Result:

value      
-----------
          1
          2
          3
          4
          5

This time it ran as expected.

As mentioned, GENERATE_SERIES() requires the database compatibility level to be at least 160. If we run it against a database with a compatibility level less than 160, the Database Engine will be unable to find the GENERATE_SERIES() function (even if we’re running it in SQL Server 2022 or higher).

Here’s how to change the compatibility level of a database. Once the database has a compatibility level of at least 160, you’ll be able to run GENERATE_SERIES() against it (assuming you’re using SQL Server 2022 or higher).