How to write a Cursor for PostgreSQL. Learn how to Open, Move, Fetch, Close., and Update/Delete DB.

Опубликовано: 04 Октябрь 2024
на канале: Software Nuggets
3,971
66

How to write a Cursor that loops over a result set, and allow the program to evaluate each row.

table of contents:
00:00 begin
00:12 - build a table and insert data
00:44 - build a basic cursor
06:42 - learn how to fetch
09:42 - learn how to loop
11:54 - learn how to loop in reverse
13:47 - learn how to update base table (and how to use random() function)
17:44 - learn how to delete from base table
21:25 - closing message from software nuggets


Access a cursor in PL/pgSQL goes through cursor variables, which are always of the special data type refcursor.
How to declare a rowset object using the database.tablename%ROWTYPE
How to open and close a cursor
How to use the cursor move method to reposition the current pointer
How to the the curosr fetch method using the directions: first, last, prior, next and relative
How to use the special variable "found" to exit a loop

Use the built-in RANDOM function and LIMIT the number of Records to be used in a Cursor.

How to use raise notice to print a response to the Messages tab.

#softwareNuggets, #postgresql