[英]Copy data from one row to another in SQL Server in the same table
I have a table that has two variations of data:我有一个包含两种数据变体的表:
Table MYDATA
表
MYDATA
Address StreetNumber Provider
----------------------------------------------
123 Main Street 1 VersionA
123 Main Street NULL VersionB
I would like to update Version A's StreetNumber into Version B.我想将版本 A 的 StreetNumber 更新为版本 B。
There will be thousands of records and the match will be on the Address column将有数千条记录,匹配将在地址列上
I thought to use:我想用:
update MYDATA
set StreetNumber = (select top 1 streetnumber
from MYDATA Goo
where Goo.Address = Address and Provider = 'VersionA')
where Provider = 'VersionB'
But it seems the nesting is not looking at the row to be updated...rather its picking one record and updating all records with the same streetnumber?但似乎嵌套没有查看要更新的行......而是选择一条记录并更新具有相同街道编号的所有记录?
This should work: 这应该工作:
UPDATE MDb
SET StreetNumber = MDa.StreetNumber
FROM MYDATA MDb
JOIN MYDATA MDa ON MDb.[Address] = MDa.[Address]
WHERE MDb.Provider = 'VersionB'
AND MDa.Provider = 'VersionA'; --You can move this clause to the ON if you prefer
Came across similar issue.遇到类似的问题。 Copying data from one row to another under the same column and inside the same table.
在同一列下和同一表内将数据从一行复制到另一行。 Maybe this will help.
也许这会有所帮助。
UPDATE MyData
SET StreetNumber = newdata.StreetNumber
FROM (
SELECT Password FROM [MyDB].[dbo].[MyData]
WHERE Provider = VersionA
)newdata
WHERE Provider = VersionB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.