26 April 2017 Sudhanshu Ranjan 563
What is cursor in SQL Server with example.
A cursor is a set of rows together with a pointer that identifies a current row.
Or
Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, its like recordset in the ASP and visual basic.
Cursor Example:
A New Table Employee_Data Created.
CREATE TABLE Employee_Data
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)
Data Inserted in Table
INSERT INTO Employee_Data VALUES ('Anees',1000);
INSERT INTO Employee_Data VALUES ('Rick',1200);
INSERT INTO Employee_Data VALUES ('John',1100);
INSERT INTO Employee_Data VALUES ('Stephen',1300);
INSERT INTO Employee_Data VALUES ('Sudhanshu Ranjan',1700);
Select * from Employee_Data
New Cursor Created -
Declare @Emp_ID as INT
Declare @Emp_name as varchar(100)
Declare @Emp_Sal as Decimal (10,2)
Declare MY_CURSOR CURSOR FOR
Select Emp_name from Employee_Data (Nolock)
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @Emp_name
WHILE @@FETCH_STATUS = 0
BEGIN
-------------Perform Task here on Row by Row Basis---------
PRINT @Emp_name--
FETCH NEXT FROM MY_CURSOR INTO @Emp_name
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
In this article I try to explain about coursor. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter
Recommended Post
- How to delete the duplicate record from table in sql server
- What is Prime Number? Print Prime Number using C#
- Create and consume a simple WCF Restful service using AngularJs application
- What is Interface in C#? Explain
- datepicker textbox in mvc 4 using JQuery
- Display data which has two or more than two times in table in sql server.
- How to reverse a String using C#
- Factorial Number Program in C#
- What is the difference between UNION and UNION ALL?
- How to save data in database using MVC in asp.net
Leave a comment
Search In This Website
POPULAR POST
- TypeError: $(...).autocomplete is not a function
- Paging in MVC using C# with razor
- How to use CKEditor in MVC application/ Asp.net application.
- Bind dropdown in MVC using jquery with C#
- How to implement CAPTCHA in MVC 4 using C# Asp.net.
- datepicker textbox in mvc 4 using JQuery
- secure connection failed firefox fix
- How to install asp.net mvc 5 in visual studio 2012
- Bind gridview and paging example in angularjs
- Create and consume a simple WCF Restful service using AngularJs application
CATEGORIES
- MVC4(26)
- Asp.net(25)
- Jquery(10)
- C#(25)
- Sql Server(9)
- Javascript(7)
- C++(5)
- Linq(1)
- Entity Framework(3)
- HTML5(5)
- Angular Js(6)
- CSS(1)
- Web API(0)
- ADO.Net(4)
- IIS(3)
- Oracle(2)
- Web Service(1)
- SSRS(0)
- SSIS(0)
- Crystal Report(0)
- My SQL(5)
- HTML(6)
- Visual Studio(18)
- WCF(1)
- WPF(0)
- XML(0)
- Knockout JS(0)
- Oops(7)
- Microsoft Office(0)
- Node JS(0)
- Windows(3)
- Angular2(2)