Use of fetch next from

Source: Internet
Author: User

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

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.