Skip to content

Instantly share code, notes, and snippets.

@erikdarlingdata

erikdarlingdata/Longingly.sql Secret

Created Nov 12, 2020
Embed
What would you like to do?
CREATE OR ALTER PROCEDURE dbo.Longingly (@loops int = 1)
AS
SET NOCOUNT, XACT_ABORT ON;
BEGIN
DECLARE @psql nvarchar(MAX) = N'DECLARE @p';
DECLARE @ssql nvarchar(MAX) = N'SELECT COUNT_BIG(*) AS records FROM dbo.Users AS u WHERE u.DisplayName IN (@p';
DECLARE @asql nvarchar(MAX) = N'';
DECLARE @i int = 1;
WHILE @i <= @loops
BEGIN
SELECT
@psql += RTRIM(@i)
+ N' NVARCHAR(40) = N'
+ QUOTENAME(CONVERT(nvarchar(36), NEWID()), '''')
+ N';'
+ NCHAR(13)
+ N'DECLARE @p';
SELECT @ssql += RTRIM(@i)
+ N', @p';
SET @i += 1;
END;
SET @psql = SUBSTRING(@psql, 1, LEN(@psql) - 10) + NCHAR(13);
SET @ssql = SUBSTRING(@ssql, 1, LEN(@ssql) - 4) + N');';
SET @asql = @psql + @ssql;
SET STATISTICS XML ON;
EXEC sys.sp_executesql @asql;
SET STATISTICS XML OFF;
END;
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment