Updated on 2024-08-20 GMT+08:00

MOVE

Description

MOVE repositions a cursor without retrieving any data. MOVE works exactly like the FETCH statement, except MOVE only repositions the cursor and does not return rows.

Syntax

1
MOVE [ direction [ FROM | IN ] ] cursor_name;

The direction clause specifies optional parameters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
NEXT
   | PRIOR
   | FIRST
   | LAST
   | ABSOLUTE count
   | RELATIVE count
   | count
   | ALL
   | FORWARD
   | FORWARD count
   | FORWARD ALL
   | BACKWARD
   | BACKWARD count
   | BACKWARD ALL

Parameters

The parameters of MOVE and FETCH are the same. For details, see Parameters in FETCH.

On successful completion, a MOVE statement returns a tag of the form MOVE count. The count is the number of rows that a FETCH statement with the same parameters would have returned (possibly zero).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- Create a table and insert data into the table.
gaussdb=# CREATE TABLE tbl_test(c1 int);
gaussdb=# INSERT INTO tbl_test VALUES (generate_series(1,20));

-- Set up cursor1.
gaussdb=# BEGIN;
gaussdb=# CURSOR cursor1 FOR SELECT * FROM tbl_test ORDER BY 1;

-- Run the MOVE command to move the cursor backwards by five rows. No result is returned.
gaussdb=# MOVE FORWARD 5 FROM cursor1;
MOVE 5

-- Run the FETCH command to retrieve two rows of data.
gaussdb=# FETCH FORWARD 2 FROM cursor1;
 c1 
----
  6
  7
(2 rows)

-- Close the cursor and end the transaction.
gaussdb=# CLOSE cursor1;
gaussdb=# END;

-- Delete.
gaussdb=# DROP TABLE tbl_test;

Helpful Links

CLOSE and FETCH