简体   繁体   English

迭代更新表中的特定列(执行批量更新)

[英]Update specific columns in a table iteratively (Do a bulk update)

My Table Schema is as follows: 我的表架构如下:

Gender: char(1), not null Last Name: varchar(25), null First Name: varhcar(35), not null 性别:char(1),not null姓:varchar(25),null名字:varhcar(35),not null

The data in the table looks like: 表中的数据如下所示:

 Gender | Last Name | First Name |

   M       Doe         John
   F       Marie       Jane
   M       Jones       Jameson
   F       Simpson     Alice

I now am trying to update all the names in the table from the names present in the txt file. 我现在正在尝试从txt文件中的名称更新表中的所有名称。

My Query is as follows: 我的查询如下:

-- 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 > 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

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. 发生的情况是表中的所有行都使用相同的值进行更新,每次执行查询时,都会使用新的值集更新它们。

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. 但是这里它在表的所有行中更新相同的随机名称。

Any help would be appreciated. 任何帮助,将不胜感激。

Each SELECT statement is only being executed once in your example (and thus returning 1 result), and since your UPDATE isn't being limited, you're applying the same value to every row. 每个SELECT语句仅在您的示例中执行一次(因此返回1个结果),并且由于您的UPDATE不受限制,因此您将相同的值应用于每一行。

If you want to update each row with different values, you can use a CTE and the ROW_NUMBER() function to update rows at a time. 如果要使用不同的值更新每一行,可以使用CTE和ROW_NUMBER()函数一次更新行。

There's no need to loop, you can do it in one fell swoop: 没有必要循环,你可以一举做到:

WITH cte AS (SELECT *,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS n1
             FROM TableName
            )
UPDATE cte 
SET FirstName = names.Name
FROM cte 
JOIN (SELECT *,ROW_NUMBER() OVER (ORDER BY NEWID()) AS n2
      FROM #name
      )names
    on cte.n1 = names.n2

Demo: SQL Fiddle 演示: SQL小提琴

This example is just for the FirstName. 此示例仅适用于FirstName。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM