Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate specific columns in a table iteratively (Do a bulk update)
    primarykey
    data
    text
    <p>My Table Schema is as follows:</p> <p>Gender: char(1), not null Last Name: varchar(25), null First Name: varhcar(35), not null</p> <p>The data in the table looks like:</p> <pre><code> Gender | Last Name | First Name | M Doe John F Marie Jane M Jones Jameson F Simpson Alice </code></pre> <p>I now am trying to update all the names in the table from the names present in the txt file. </p> <p>My Query is as follows: </p> <pre><code>-- Sort out the Forenames we'll be using for the data, we make a #Name2 table because I have yet to figure our -- inserting specific columns using BULK INSERT and without using a format file. CREATE TABLE #Name (Name VARCHAR(50)) CREATE TABLE #ForeNames (FirstName VARCHAR(50), Gender VARCHAR(1)) -- Move data in the #Name2 table BULK INSERT #Name FROM "c:\girlsforenames.txt" WITH (ROWTERMINATOR='\n') -- Now move it to the forename table and add the gender INSERT INTO #ForeNames SELECT [Name], 'F' FROM #Name -- Delete the names from temporary table TRUNCATE TABLE #Name -- Same for the boys BULK INSERT #Name FROM "c:\boysforenames.txt" WITH (ROWTERMINATOR='\n') INSERT INTO #ForeNames SELECT [Name], 'M' FROM #Name -- Now do the surnames TRUNCATE TABLE #Name BULK INSERT #Name FROM "c:\surnames.txt" WITH (ROWTERMINATOR='\n') DECLARE @Counter BIGINT SET @Counter = 4 WHILE (@Counter &gt; 0) BEGIN UPDATE TableName set [last_name]= (SELECT TOP 1 FirstName from #ForeNames), [first_name]=(SELECT TOP 1 Name FROM #Name ORDER BY NEWID()), [gender]= ( SELECT TOP 1 Gender FROM #ForeNames ORDER BY NEWID()); SET @Counter=@Counter-1 END DROP TABLE #Name DROP TABLE #ForeNames SELECT * FROM TableName </code></pre> <p>What Happens is all the rows in the table are updated with the same values and each time i execute the query they are updated with the new set of values.</p> <p>What I want is to loop through each row and update it and den update the next row with the other set of random name. But here it is updating the same random name across all the rows of the table.</p> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload