ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • Hosting ASP.NET Core Web API on IIS
  • Use of TOP and DISTINCT in SQL SERVER
  • Difference between char, nchar, varchar, and nvarchar in MS SQL Server
  • What's new in SQL Server 2019?
  • Uploading .csv sample table file data to SQL Server
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

SQL Server Stored Procedures: Part-3

Server SQL Server about 7 years ago || 6/6/2019 || 3.0 K View

  • Hide

SQL Server CURSOR

For SQL Server Stored Procedures: Part-2, Click here.

Database cursor act like a pointer in a database. It process query on each row one by one. Cursor act like a pair of tweezers to pick up each row of data from bag of data. You should use cursor if you have large amount of data and need to act individually on each row. Cursor doesn’t need to allocate big chunk of space in server to load result set like in SQL.

Life cycle of SQL Server cursor:

Step for using a cursor:

 

First, Declare variables and cursor

DECLARE  cursorName  AS CURSOR

To declare cursor you should use DECLARE keyword followed by cursorName with its data type as CURSOR and SELECT statement for defining its result set.

Second, Open cursor

OPEN CURSOR cursorName

After SELECT statement open CURSOR using OPEN CURSOR keyword with cursorName.

Third, fetch a row form cursor into one or more variable

FETCH NEXT CURSOR FROM cursorName INTO variables

Fetch function use @@FETCHSTATUS to returns status of the last cursor. If @@FETCHSTATUS returns 0, it means fetching is successful. We can use WHILE statement to fetch each rows which can be done as:

WHILE @@FETCHSTATUS = 0
      BEGIN
         FETCH NEXT FROM cursorName
      END

Fourth, close the cursor

CLOSE cursorName

Lastly, deallocate the cursor

DEALLOCATE cursorName

Deallocation give permission to use same cursor name once again.

For Example:

DECLARE
     @firstName VARCHAR(MAX),
     @salary DECIMAL
DECLARE empCursor CURSOR
       FOR SELECT
           First_Name,
           Salary
       FROM
           dbo.EmployeeDb

OPEN empCursor

FETCH NEXT FROM empCursor INTO
           @firstName,
           @salary
      WHILE @@FETCH_STATUS = 0
            BEGIN
              PRINT @firstName +CAST(@salary AS VARCHAR)
              FETCH FROM empCursor INTO
                    @firstName,
                    @salary
            END

CLOSE empCursor

DEALLOCATE empCursor

Syntax description:

First, Declare two variable to hold First_Name and Salary, cursor name as empCursor  to hold the result of query that select name and salary from dbo.EmployeeDb.

Then, Open cursor as

OPEN empCursor

Next, fetch each rows from cursor and print the name and salary of employee.

FETCH NEXT FROM empCursor INTO
           @firstName,
           @salary 
           WHILE @@FETCH_STATUS = 0
                 BEGIN
                    PRINT @firstName +CAST(@salary AS VARCHAR)
                    FETCH FROM empCursor INTO
                          @firstName,
                          @salary
                 END

Next, close the cursor

CLOSE empCursor

At Last, deallocate cursor

DEALLOCATE empCursor

Output:

  For SQL Server Stored Procedure: Part-4, Click here.

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

MAT RIX

MAT RIX

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey