title: T-SQL: create stored procedure to update column in table with table and field as params in
body: <p>I want to cleanse some columns in a table of a massive database with random data. I have created a stored procedure <code>CreateRandomString</code> 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).</p>
  
  <p>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.</p>
  
  <p>What I have so far:</p>
  
  <pre><code>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)
      )
  </code></pre>
  
  <p>And to get a random string I just do:</p>
  
  <pre><code>EXEC CreateRandomString @RandomString = @RandomString OUTPUT
  </code></pre>
  
  <p>However, I can't use an <code>EXEC</code> 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?</p>