Skip to content

Instantly share code, notes, and snippets.

@viveret
Last active August 7, 2019 19:05
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 viveret/8fd434a3044f62ab329a445504874a6a to your computer and use it in GitHub Desktop.
Save viveret/8fd434a3044f62ab329a445504874a6a to your computer and use it in GitHub Desktop.
Template script for updating sanitized messages with sample text from a file for SQL Server
sp_configure 'show advanced options',1
reconfigure;
GO
sp_configure 'Ad Hoc Distributed Queries',1
reconfigure;
GO
CREATE TABLE SampleText (
[text] VARCHAR(MAX)
);
GO
BULK INSERT SampleText
FROM 'C:\Users\viveret steele\Downloads\samples.txt'
WITH
(
FIELDTERMINATOR ='$$$$$$$',
ROWTERMINATOR ='\n'
);
GO
sp_configure 'Ad Hoc Distributed Queries',0
reconfigure;
GO
sp_configure 'show advanced options',0
reconfigure;
GO
--declare @subjectSampleCount int = (SELECT COUNT(*) FROM [ViveretsData].dbo.SampleText WHERE LEN([text]) < 30)
--declare @bodySampleCount int = (SELECT COUNT(*) FROM [ViveretsData].dbo.SampleText WHERE LEN([text]) >= 30)
declare @amount int = 1000;
declare @start int = @amount * 1;
WITH SubjectSamples AS (
SELECT TOP 1494 *, ROW_NUMBER() OVER (ORDER BY NEWID()) [rand]
FROM [ViveretsData].dbo.SampleText WHERE LEN([text]) < 30 ORDER BY [rand]
),
BodySamples AS (
SELECT TOP 13068 *, ROW_NUMBER() OVER (ORDER BY NEWID()) [rand]
FROM [ViveretsData].dbo.SampleText WHERE LEN([text]) >= 30 ORDER BY [rand]
)
UPDATE m
SET m.[Body] = bs.[text],
m.[Subject] = ss.[text]
FROM Messages m
INNER JOIN SubjectSamples ss ON ss.[rand] = m.Id % 1494 + 1
INNER JOIN BodySamples bs ON bs.[rand] = m.Id % 13068 + 1
WHERE m.Id BETWEEN @start AND @start + @amount;
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment