Original table structure:
Hr_newspaper
------------------
ID
CompanyName
Companyinfo
Positionname
Positionnumber
Requirement
Note
To eliminate redundant data, the table is divided into two tables, company is used to store company information, and hrnewspaper is used to store recruitment information.
Hrnewspaper table:
------------------
ID
Companyid
Positionname
Positionnumber
Requirement
Infofrom
Updatetime
Company table:
------------------
ID
CompanyName
Companyinfo
Note
Infofrom
Updatetime
There are three methods to exclude company's redundant data in the original table (only key statements are listed ):
1. Select distinct companyName into TMP from hr_newspaper
2. Select ID, companyName, companyinfo, note, getdate () as updatetime into companynew from hr_newspaper T1
Where checksum (*) = (select top 1 checksum (*) from hr_newspaper where companyName = t1.companyname)
3. Select min (ID) as ID, companyName, min (companyinfo) as companyinfo, min (Note) as note, getdate () as updatetime into newcompany
From hr_newspaper group by companyName
Use fetch next to cyclically retrieve relevant data and insert a new table:
Declare @ companyName varchar (256)
Declare @ companyinfo varchar (4000)
Declare @ positionname varchar (60)
Declare @ positionnumber int
Declare @ requirement varchar (4000)
Declare @ note varchar (4000)
Declare @ ID int
Declare @ tmphrid int
Declare @ counter int
Declare @ I int
Set @ ID = 1
Set @ I = 1
Select @ counter = count (companyName) from hr_newspaper
Declare hr_cursor cursor
Select ID, companyName, companyinfo, positionname, positionnumber, requirement, note from hr_newspaper
Open hr_cursor
Fetch next from hr_cursor
Into @ ID, @ companyName, @ companyinfo, @ positionname, @ positionnumber, @ requirement, @ note
While @ fetch_status = 0
Begin
Print convert (varchar (4), @ ID) + ',' + @ companyName + ',' + @ positionname
Select @ I = ID from company where companyName = @ companyName
-- Insert data
Insert into hrnewspaper (companyid, positionname, positionnumber, requirement)
Values (@ I, @ positionname, @ positionnumber, @ requirement)
-- This is executed as long as the previous fetch succeeds.
Fetch next from hr_cursor
Into @ ID, @ companyName, @ companyinfo, @ positionname, @ positionnumber, @ requirement, @ note
End
Close hr_cursor
Deallocate hr_cursor