What is a cursor?

Source: Internet
Author: User
Tags how to use sql

What is a cursor?

Declare
Object_name varchar2 (24 );
Type ref_cursor_type is ref cursor;
Cursor_object ref_cursor_type;
Begin

Open cursor_object
'
Select regexp_substr (''3h, 4c, 5e, 6b, 7a, 8g'', ''[^,] +'', 1, level)
From dual
Connect by level <= regexp_count (''3h, 4c, 5e, 6b, 7a, 8g'', '','') + 1
';

Loop
Begin
Fetch cursor_object into object_name;
Exit when cursor_object % notFound;

If object_name = '4c 'then
Dbms_output.put_line ('good: '| object_name );
End if;
End;
End loop;
End;


How to use SQL cursors

A. Use FETCH in A simple cursor
In the following example, a simple cursor is declared for the row whose last name starts with B in the authors table, and these rows are extracted one by one using FETCH NEXT. The FETCH statement returns the value of the column specified by declare cursor in the form of a single row result set.

USE pubs
GO
DECLARE authors_cursor CURSOR
SELECT au_lname FROM authors
WHERE au_lname LIKE "B %"
Order by au_lname

OPEN authors_cursor

-- Perform the first fetch.
Fetch next from authors_cursor

-- Check @ FETCH_STATUS to see if there are any more rows to fetch.
WHILE @ FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
Fetch next from authors_cursor
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

Au_lname
----------------------------------------
Bennet
Au_lname
----------------------------------------
Blotchet-Hils
Au_lname
----------------------------------------

B. Use FETCH to store values into Variables
The following example is similar to the preceding example, but the output of the FETCH statement is stored in local variables instead of directly returned to the client. The PRINT statement combines variables into a single string and returns them to the client.

USE pubs
GO

-- Declare the variables to store the values returned by FETCH.
DECLARE @ au_lname varchar (40), @ au_fname varchar (20)

DECLARE authors_cursor CURSOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B %"
Order by au_lname, au_fname

OPEN authors_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- In the SELECT statement.

Fetch next from authors_cursor
INTO @ au_lname, @ a ...... the remaining full text>

How to use SQL cursors

A. Use FETCH in A simple cursor
In the following example, a simple cursor is declared for the row whose last name starts with B in the authors table, and these rows are extracted one by one using FETCH NEXT. The FETCH statement returns the value of the column specified by declare cursor in the form of a single row result set.

USE pubs
GO
DECLARE authors_cursor CURSOR
SELECT au_lname FROM authors
WHERE au_lname LIKE "B %"
Order by au_lname

OPEN authors_cursor

-- Perform the first fetch.
Fetch next from authors_cursor

-- Check @ FETCH_STATUS to see if there are any more rows to fetch.
WHILE @ FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
Fetch next from authors_cursor
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

Au_lname
----------------------------------------
Bennet
Au_lname
----------------------------------------
Blotchet-Hils
Au_lname
----------------------------------------

B. Use FETCH to store values into Variables
The following example is similar to the preceding example, but the output of the FETCH statement is stored in local variables instead of directly returned to the client. The PRINT statement combines variables into a single string and returns them to the client.

USE pubs
GO

-- Declare the variables to store the values returned by FETCH.
DECLARE @ au_lname varchar (40), @ au_fname varchar (20)

DECLARE authors_cursor CURSOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B %"
Order by au_lname, au_fname

OPEN authors_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- In the SELECT statement.

Fetch next from authors_cursor
INTO @ au_lname, @ a ...... the remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.