Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 645691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:31:51+00:00 2026-05-13T21:31:51+00:00

I’ve got about 25 tables that I’d like to update with random data that’s

  • 0

I’ve got about 25 tables that I’d like to update with random data that’s picked from a subset of data. I’d like the data to be picked at random but meaningful — like changing all the first names in a database to new first names at random. So I don’t want random garbage in the fields, I’d like to pull from a temp table that’s populated ahead of time.

The only way I can think of to do this is with a loop and some dynamic sql.

  1. insert pick-from names into temp table
    with id field
  2. foreach table name in a list of
    tables:

    1. build a dynamic sql that updates all
      first name fields to be a name
      picked at random from the temp table based on rand() * max(id) from temp table

But anytime I think “loop” in SQL I figure I’m doing something wrong.

The database in question has a lot of denormalized tables in it, so that’s why I think I’d need a loop (the first name fields are scattered across the database).

Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:31:52+00:00Added an answer on May 13, 2026 at 9:31 pm

    Breaking the 4th wall a bit by answering my own question.

    I did try this as a sql script. What I learned is that SQL pretty much sucks at random. The script was slow and weird — functions that referenced views that were only created for the script and couldn’t be made in tempdb.

    So I made a console app.

    1. Generate your random data, easy
      to do with the Random class (just
      remember to only use one instance of
      Random).
    2. Figure out what columns and table
      names that you’d like to update via
      a script that looks at
      information_schema.
    3. Get the IDs
      for all the tables that you’re going
      to update, if possible (and wow will
      it be slow if you have a large table
      that doesn’t have any good PKs).
    4. Update each table 100 rows at a time. Why 100? No idea. Could be 1000. I just picked a number. Dictionary is handy here: pick a random ID from the dict using the Random class.

    Wash, rinse, repeat. I updated about 2.2 million rows in an hour this way. Maybe it could be faster, but it was doing many small updates so it didn’t get in anyone’s way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.