UrbanPro
true

Learn MS SQL from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Cursors In SQL Server

First thing first Usage of Cursors is not encouraged in SQL Server as they are slow. You may go with While loop if you need to iterate through a recordset. Cursor is a database object to retrieve data from a result set one row at a time, instead of the T-SQL commands that operate on all the rows in the result set at one time.So this is a row by row operation instead of a set based operation.
 
 
SQL Server Cursor Components: Cursors include following components:
 
1) Declare statements: Declare variables used in the code block.
 
2) Set\Select statements: Initialize the variables to a specific value.
 
3) Declare Cursor statement: Populate the cursor with values that will be evaluated.
 
4) Open statement: Open the cursor to begin data processing.

5) Fetch Next statements: Assign the specific values from the cursor to the variables

Note: This logic is used for the initial population before the While statement and then again during each loop in the process as a portion of the While statement.

While statement: Condition to begin and continue data processing.

Begin...End statement: Start and end of the code block.
 
Close statement: Releases the current data and associated locks, but permits the cursor to be re-opened.

Deallocate statement: Destroys the cursor
 
Below is an example of a static cursor:
 
DECLARE @Id int
DECLARE @name varchar(50)
DECLARE @salary int
DECLARE cur_emp CURSOR STATIC FOR
SELECT EmpID,EmpName,Salary from ContractEmployee
OPEN cur_emp
FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
--FETCH ABSOLUTE 3 FROM cur_emp INTO @Id,@name,@salary
WHILE @@Fetch_status = 0
BEGIN
PRINT 'ID : '+ convert(varchar(20),@Id)+', Name : '+@name+ ', Salary : '+convert(varchar(20),@salary)
--FETCH RELATIVE 3 FROM cur_emp INTO @Id,@name,@salary
FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
END
CLOSE cur_emp
DEALLOCATE cur_emp

 
0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

What's the best way to learn SQL Server?
Just read the book, type and run the code, and work the chapter examples. If you work the book, you will be ahead of many SQL Server programmers. You’ll understand declarative set-based interaction...

An Introduction to Business Intelligence Concepts
Looking for a Business Intelligence (BI) solution for your company can be intimidating. BI uses its own special terminology and the database design concepts can be difficult to grasp. So where do you...

SQL Tips (4 to 6)
SQL tips 4:Avoid INDEX, unless you need to retrieve information quickly. Index will slower insert and update data query.The another way is using sub querySelect MAX(salary)FROM employeeWHERE salary IN(Select...

What DBA should do to avoid database from running out of space?
Disk space is something that really critical for SQL Server, and if we don’t keep a close eye on available disk resources, it could cause database failures or hamper depended on the major business...

SQL Server – CAST/CONVERT to string – Pad zeroes or spaces to an integer
Recently, I came across quite a common question – as part of a data migration, someone wanted to pad integers with zeroes. There are various variations to this question, namely: How do I pad zeroes...
X

Looking for MS SQL Classes?

The best tutors for MS SQL Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn MS SQL with the Best Tutors

The best Tutors for MS SQL Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more