MSSQL uses cursor traversal to update data in bulk update

Source: Internet
Author: User
Tags mssql

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.