What is a Cursor in SQL? - Example & Syntax

Instructor: David Gloag
Being able to locate and manipulate specific pieces of information is important today. In this lesson, we'll take a look at SQL, cursors in SQL, an example, and the syntax associated with a cursor.

The Use of Information

Information is all around us. We capture it when something happens that needs our attention, we access it when we want to arrive at a conclusion or derive a hidden meaning, and we update it when it becomes old or obsolete. In some respects, we're addicted to information. But how do we handle tasks like these in an organized fashion? We have so much information, that it seems to be impossible. But the truth is that it isn't. Technology exists that can help us pinpoint a specific piece, or examine the information as a whole. That technology is called SQL.

What is SQL?

Using a complement of commands, SQL or Structured Query Language, allows you to perform various operations on a database. For example, you can store, retrieve, search, and sort information, among other things. Having been around for many years, SQL was first conceived by IBM in the early 1970's, and it became a commercial product when it was released by Oracle in 1979. Since then, it has gained popular acceptance and is now used widely. Some companies include it as part of their own product offerings.

What is a Cursor?

A cursor is a variable that indicates a position or location within a database. Consider a Relational Database Management System (RDBMS) like Microsoft SQL Server, which is an RDBMS that stores information in tables, where each row is a record, and each column is a field in the record. A cursor would point to a specific record in a specific table within the database. Think of cursors like the mouse pointer on your computer screen. The arrow (assuming you haven't changed it), shows the current location for any action or the hotspot, so to speak. A database can have more than one cursor, and each can point to a different part of the database.

What is a Cursor Used for?

For the most part, cursors aren't necessary and you can access the information you need without them. But there are times when multiple records are returned as the result of a query. In these cases, you may often want to access the result records one by one. Cursors provide the means to do that and allow you to traverse records in a database. By traverse, we mean that you can access records in sequence, although, that sequence may not necessarily match the order that the records are physically stored in.

What Does SQL Cursor Syntax Look Like?

In Microsoft Transact-SQL (SQL Server's variant), cursors affect a number of commands. They include:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account