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 is Hyperion?
- Its an Business Intelligence tools. Like Brio which was an independent product bought over my Hyperion has converted this product name to Hyperion Intelligence. Is it an OLAP tool? - Yes. You can analyse...

Top 10 SQL Concepts for Job Interview
1. SELECT and FROM: This is the “heart” of any SQL query - SELECT columns FROM table.2. WHERE: This acts as a filter and allows you to select only relevant rows based on conditions.3. GROUP...

How To Insert The Result Sets Of A Stored Procedure/Functions Into A Temporary Table In SQL Server?
How to Insert the Result sets of a Stored Procedure/Functions into a Temporary Table in SQL Server In some cases with SQL Server, there may be an instance where you wish to take the resulting data from...

Microsoft Outlook
Microsoft Outlook is the preferred email client used to access Microsoft Exchange Server email. Not only does Microsoft Outlook provide access to Exchange Server email, but it also includes contact, calendaring...

WebSphere
WebSphere is a set of Java-based tools from IBM that allows customers to create and manage sophisticated business Web sites. The central WebSphere tool is theWebSphere Application Server (WAS), an application...
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