Explain about what is Cursor in SQL Server? Write a SQL cammand to create cursor in SQL Server
Explain about what is Cursor in SQL Server? Write a SQL cammand to create cursor in SQL Server
Software Developer
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer
SQL Cursor :
Oracle creates a memory area, also called context area, to process SQL statements. Cursor is a pointer to this memory area and this memory (context) area is controlled by the cursor.
The size of the cursor is flexible according to its data. Oracle keeps some predefined space in the main memory to open the cursor, so the size of the cursor is limited. Whenever a query is run, the cursor works.
A Cursor is fetch all rows of a table one by one selected by the SQL statements. When I have need to check all rows of a table one by one or update data of that table then here we use Cursor.
Types of Cursor:
There are two types of Cursor,
1- Implicit Cursor.
2- Explicit Cursor.
Implicit cursor: Whenever the SQL statement is executed, the implicit cursor is automatically created by Oracle (when there is no explicit cursor for the statement.)
'Programmers cannot control the implicit cursor and the information contained in it”. The cursor that is opened by Oracle for internal processing is called implicit cursor.
Explicit cursor: Explicit cursor is user-defined. Through Explicit cursor, we can have more control in the memory (context) area.
The cursor is used when some records from a table are accessed in a PL/SQL code block. SQL queries are used to declare this cursor.
Both these types of cursor have four common attributes, which are as follows:-
%isopen:- Returns true value if the cursor is open otherwise return false.
%found:- Returns true value if records are fetched successfully otherwise return false.
%notfound:- Returns true value if the records are not fetched successfully otherwise return false.
%Rowcount:- It returns the number of records processed.
Create a Cursor:
Ex- Lets have take a database table ‘Student’
The following SQL statement is used cursor to retrieve the student ID and Name from above table.
The following SQL statement is used to create a Cursor to fetch the distinct record from two table and show all records into a single form.