I want to cleanse some columns in a table of a massive database with random data. I have created a stored procedure CreateRandomString
to create random data (to replace client names etc so we can use the data as a demo). Now I want to create the stored procedure that will take a table name and a column name and replace all the data in that column with random values (each column with a different random value).
I have done mySQL and Oracle in the past, but am now working in SQL Server, and I am stuck with the updating etc. I will not know the key etc on the table, as this is passed in as a parameter.
What I have so far:
CREATE OR ALTER PROC SanitiseData
(@DataColumnName NVARCHAR(MAX),
@DataTableName NVARCHAR(MAX))
AS
BEGIN
DECLARE @RandomString NVARCHAR(MAX)
;WITH r AS
(
EXEC('SELECT ' + @DataColumnName + ', rn = ROW_NUMBER() OVER (ORDER BY ' + @DataColumnName + ') - 1
FROM ' + @DataTableName)
)
And to get a random string I just do:
EXEC CreateRandomString @RandomString = @RandomString OUTPUT
However, I can't use an EXEC
there, so how can I use variables for table and field names, and then how can I loop through and update each row with a different value?