Skip to content

Instantly share code, notes, and snippets.

@alexandrebl
Created April 14, 2018 21:27
Show Gist options
  • Select an option

  • Save alexandrebl/d8dc511b6fe5cc9ffc352114ea487e5d to your computer and use it in GitHub Desktop.

Select an option

Save alexandrebl/d8dc511b6fe5cc9ffc352114ea487e5d to your computer and use it in GitHub Desktop.
SQL Server Batch Writter
IF OBJECT_ID('dbo.Nums') IS NOT NULL
DROP TABLE dbo.Nums;
GO
CREATE TABLE dbo.Nums(n INT NOT NULL PRIMARY KEY);
DECLARE @max AS INT, @rc AS INT;
SET @max = 1000000000;
SET @rc = 1;
INSERT INTO Nums VALUES(1);
WHILE @rc * 2 <= @max
BEGIN
INSERT INTO dbo.Nums SELECT n + @rc FROM dbo.Nums;
SET @rc = @rc * 2;
END
INSERT INTO dbo.Nums
SELECT n + @rc FROM dbo.Nums WHERE n + @rc <= @max;
DECLARE @s AS DATETIME, @e AS DATETIME;
SET @s = '19800101';
SET @e = '21061231';
SELECT @s + n - 1 AS dt
FROM dbo.Nums
WHERE n <= DATEDIFF(day, @s, @e) + 1;
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment