Original table structure:
Hr_newspaper
------------------
Id
CompanyName
CompanyInfo
Positionname
Positionnumber
Requirement
Note
To exclude redundant data, the table is divided into two tables, which are used to store company-related information, and table 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 ways to exclude redundant data about company 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 ", GETDATE () as updatetime into Ne Wcompany
From Hr_newspaper GROUP by CompanyName
Use fetch next to loop out the relevant data and insert the 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 FOR
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
--Inserting data
INSERT into Hrnewspaper (CompanyID, Positionname, Positionnumber, requirement)
VALUES (@i, @PositionName, @PositionNumber, @Requirement)
--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