Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SQL Server generate_series
-- http://blog.jooq.org/2013/11/19/how-to-create-a-range-from-1-to-10-in-sql/
IF EXISTS (SELECT *
FROM dbo.sysobjects
WHERE id = object_id (N'[dbo].[generate_series]')
AND OBJECTPROPERTY(id, N'IsTableFunction') = 1)
DROP FUNCTION [dbo].[generate_series]
GO
CREATE FUNCTION [dbo].[generate_series] ( @p_start INT, @p_end INT)
RETURNS @Integers TABLE ( [IntValue] INT )
AS
BEGIN
WITH interval(V) AS (
SELECT @p_start
UNION ALL
SELECT V + 1 FROM interval
WHERE V < @p_end
)
INSERT INTO @Integers
SELECT * FROM interval;
RETURN
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.