Use of FETCH Next

Source: Internet
Author: User
Tags getdate

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

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.