Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voronoipotato/ff284c6292c36614c147d5aa0ea489ce to your computer and use it in GitHub Desktop.
Save voronoipotato/ff284c6292c36614c147d5aa0ea489ce to your computer and use it in GitHub Desktop.
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