Batch update through public table in mssql

Business requirements: The contents stored in the phone field in table A need to be updated in batches to the mobile phone number
insert image description here
connection table to query the correct mobile phone number and store it in the temporary table

;WITH cte AS(
SELECT a.*,b.phone AS phone1,b.ID AS userid FROM Table_A AS a LEFT JOIN Table_User_Info AS b
ON a.phone=CONVERT(VARCHAR(100),b.id) WHERE a.phone <>'' AND a.phone NOT LIKE '%*%'AND LEN(B.phone)=11
)SELECT * FROM cte

在表中新增一个finalphone字段,内容为空,最后将正确信息更新进finalphone字段中
insert image description here
At this time, the phone1 field is the last mobile phone number that should be updated
批量更新

;WITH cte AS(
SELECT a.*,b.phone AS phone1,b.ID AS userid FROM Table_A AS a LEFT JOIN Table_User_Info AS b
ON a.phone=CONVERT(VARCHAR(100),b.id) WHERE a.phone <>'' AND a.phone NOT LIKE '%*%'AND LEN(B.phone)=11
)UPDATE cte SET FinalPhone= phone1 WHERE phone1 IS NOT NULL

update completed
insert image description here

Guess you like

Origin blog.csdn.net/zyue_1217/article/details/125974082