Last active
July 28, 2019 00:12
-
-
Save sebastianwebber/5dac286e961d73badbe6 to your computer and use it in GitHub Desktop.
SQL Server generate_series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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