MSSQL uses cursor traversal to update data in bulk update
/*
--1. Create a test table
Create TABLE Table1
(
A varchar (10),
b varchar (10),
C varchar (10),
CONSTRAINT [Pk_table1] PRIMARY KEY CLUSTERED
(
A ASC
)
) on [PRIMARY]
Create TABLE Table2
(
A varchar (10),
C varchar (10),
CONSTRAINT [Pk_table2] PRIMARY KEY CLUSTERED
(
A ASC
)
) on [PRIMARY]
Go
--2. Create test data
Insert into Table1 values (' Zhao ', ' ASDs ', null)
Insert into Table1 values (' money ', ' ASDs ', ' 100 ')
Insert into Table1 values (' Sun ', ' ASDs ', ' 80 ')
Insert into Table1 values (' Lee ', ' ASDs ', null)
INSERT into Table2 values (' Zhao ', ' m ')
Insert into Table2 values (' money ', ' m ')
Ins ert into Table2 values (' Sun ', ' m ')
Insert into Table2 values (' Lee ', ', ')
go
&NB Sp SELECT * from table1
--3. Update by cursor
DECLARE @name varchar (a)
DECLARE @score varchar (a)
DECLARE mycursor cursor for select a to Table1 where C is null
open mycursor
FETCH NEXT from MyCursor to @name
while (@ @fetch_status = 0)
begin
Select @score =c from Table2 where a= @name
Update Table1 Set c = @score where a = @name
; FETCH NEXT from MyCursor to @name
end
Close mycursor
Dealloc Ate mycursor
go
--4. Show updated results
SELECT * from table1
go
--5. Delete Test table
DROP table table1
drop table table2
Although it can be implemented with cursors, the code looks very complex
Update is a simpler statement in T-SQL, and the Update table set column=expression [WHERE condition], we all use it. But the use of update is not only this, really in the development of the time, flexibly and appropriately using the update can achieve a multiplier effect.
Assuming that there are table Table1 (a,b,c) and Table2 (A,c), some record field C in Table1 is NULL, and you want to update Table2 by looking in the field a in Table1, and by taking the value of field C, which is equal to field a. A conventional way of thinking, through a cursor traversing all records of NULL in field C in Table1, looking for Table2 in the loop body and updating it in the form of a cursor cursor
*/