title: How to choose a random ID from a table and use it for populate random data to another table?
body: <p>I'm trying to insert random data to do a stress test for my application, following this <a href="https://www.mssqltips.com/sqlservertip/5148/populate-large-tables-with-random-data-for-sql-server-performance-testing/" rel="nofollow noreferrer">guideline</a>.</p>
  
  <p>I've some foreign key on some columns, and I want to insert some random data for those too.</p>
  
  <p>Let say table <code>Clinics</code> have 5 records, with ID "3,7,90,98,102"; for each insert on <code>Activities</code> I'd like to pick one of those 5 IDs. Tried with:</p>
  
  <pre><code>Declare @clinicId int
  
  Declare @counter int
  Set @counter = 11
  
  While @counter &lt; 12
  Begin 
      @clinicId = SELECT TOP 1 * FROM Clinics ORDER BY NEWID()
  
     Insert Into Activities values (@clinicId, 100, 10, 90, 2, 65, 1)
  
     Set @counter = @counter + 1
  End
  </code></pre>
  
  <p>But it says <code>Syntax error in proximity of @clinicId</code>. Any clues?</p>