Now there are two tables to insert data in Table A into Table B. They all have three fields, and the fields in Table A do not have a primary key, and there are also repeated data fields, however, this field in Table B does not need to be repeated and must be unique. At this time, I thought of a simple and fast way to use the cursor to read data from one row and one row, insert the read data into a temporary table. In the temporary table, I set a self-increasing ID so that the duplicate can be eliminated through the same ID (that is, through the maximum or minimum ID method)CodeExample:
Select * From declare @ icard varchar (20)
Declare @ num varchar (20)
Declare @ intid varchar (20)
Declare @ newtable table (ID int identity (1, 1), icard varchar (20), INAME varchar (20), iage varchar (20 ))
Select Identity (INT, 1, 1) as ID, * into newtable from
Declare cursor_xs cursor
Select icard, INAME, iage from newtable where ID in (select max (ID) from newtable group by [icard])
Open cursor_xs
Fetch next from cursor_xs into @ icard, @ num, @ intid
While @ fetch_status = 0
Begin
Insert into B values (@ icard, @ num, @ intid)
Fetch next from cursor_xs into @ icard, @ num, @ intid
End
Close cursor_xs
Deallocate cursor_xs
Drop table newtable