Created
November 7, 2020 22:47
-
-
Save oskaremil/4f9df7a732735b5c977220c81d60e371 to your computer and use it in GitHub Desktop.
Creates two parent-child tables and populates them with random data
This file contains 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
-- Create a procedure that returns random integers. | |
IF OBJECT_ID('sp_RandomInt', 'P') IS NOT NULL | |
DROP PROCEDURE sp_RandomInt; | |
GO | |
CREATE PROCEDURE sp_RandomInt | |
@lower INT = 1, | |
@upper INT, | |
@randomInt INT = NULL OUTPUT | |
AS | |
BEGIN | |
SET NOCOUNT ON; | |
DECLARE @result INT; | |
SET @randomInt = FLOOR(RAND()*(@upper-@lower+1))+@lower; | |
END | |
GO | |
-- Create a procedure that returns random strings | |
IF OBJECT_ID('sp_RandomString', 'P') IS NOT NULL | |
DROP PROCEDURE sp_RandomString; | |
GO | |
CREATE PROCEDURE sp_RandomString | |
@minLength INT = 1, | |
@maxLength INT, | |
@randomString VARCHAR(MAX) OUTPUT | |
AS | |
BEGIN | |
DECLARE @charIndex INT; | |
DECLARE @chars VARCHAR(68) = 'QWERTYUIOPÅASDFGHJKLØÆZXCVBNMqwertyuiopåasdfghjkløæzxcvbnm1234567890'; | |
DECLARE @stringLength INT; | |
SET @randomString = ''; | |
-- generate a random string length | |
EXEC dbo.sp_RandomInt @lower = @minLength, @upper = @maxLength, @randomInt = @stringLength OUTPUT; | |
WHILE LEN(@randomString) < @stringLength | |
BEGIN | |
-- generate a random char index | |
EXEC dbo.sp_RandomInt @upper = 68, @randomInt = @charIndex OUTPUT; | |
SET @randomString = CONCAT(@randomString , SUBSTRING(@chars, @charIndex, 1)); | |
END | |
END | |
GO | |
IF OBJECT_ID('[Children]', 'U') IS NOT NULL | |
DROP TABLE [Children]; | |
IF OBJECT_ID('[Parents]', 'U') IS NOT NULL | |
DROP TABLE [Parents]; | |
CREATE TABLE [Parents] | |
( | |
[Id] INT IDENTITY(1,1) PRIMARY KEY, | |
[Name] NVARCHAR(50) NOT NULL, | |
[Description] NVARCHAR(50) NOT NULL | |
); | |
CREATE TABLE [Children] | |
( | |
[Id] INT IDENTITY(1,1) PRIMARY KEY, | |
[Name] NVARCHAR(50) NOT NULL, | |
[Description] NVARCHAR(50) NOT NULL, | |
[ParentId] INT NULL FOREIGN KEY REFERENCES Parents(Id) | |
); | |
GO | |
DECLARE @parentRows INT = 20; | |
DECLARE @childRows INT = 60000; | |
DECLARE @i INT; | |
-- Insert parent rows | |
SET NOCOUNT ON; | |
SET @i = 0; | |
DECLARE @parentName VARCHAR(50); | |
DECLARE @parentDescription VARCHAR(50); | |
WHILE @i < @parentRows | |
BEGIN | |
EXEC dbo.sp_RandomString @minLength = 10, @maxLength = 50, @randomString = @parentName OUTPUT; | |
EXEC dbo.sp_RandomString @minLength = 30, @maxLength = 50, @randomString = @parentDescription OUTPUT; | |
INSERT INTO Parents ([Name], [Description]) VALUES (@parentName, @parentDescription); | |
SET @i = @i + 1; | |
END; | |
-- Insert child rows | |
SET @i = 0; | |
DECLARE @childName VARCHAR(50); | |
DECLARE @childDescription VARCHAR(50); | |
DECLARE @randomInt INT; | |
DECLARE @upper INT = @parentRows -1; | |
DECLARE @parentId INT; | |
WHILE @i < @childRows | |
BEGIN | |
EXEC dbo.sp_RandomString @minLength = 10, @maxLength = 50, @randomString = @childName OUTPUT; | |
EXEC dbo.sp_RandomString @minLength = 30, @maxLength = 50, @randomString = @childDescription OUTPUT; | |
EXEC dbo.sp_RandomInt @lower = 0, @upper = @upper, @randomInt = @randomInt OUTPUT; | |
SET @parentId = (SELECT Id FROM Parents ORDER BY Id OFFSET @randomInt ROWS FETCH NEXT 1 ROWS ONLY); | |
-- Let some parent Ids be null | |
EXEC dbo.sp_RandomInt @lower = 0, @upper = 10, @randomInt = @randomInt OUTPUT; | |
IF(@randomInt = 10) BEGIN SET @parentId = NULL END; | |
INSERT INTO Children ([Name], [Description], [ParentId]) VALUES (@childName, @childDescription, @parentId); | |
SET @i = @i + 1; | |
END; | |
SET NOCOUNT OFF; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment