Create a cursor in SQL

A cursor can be created using the DECLARE statement, most commonly with the name of the cursor and takes a SELECT statement and optional WHERE and other clauses. Below is an example of creating a cursor in DB2, MariaDB, MySQL, and SQL Server:

DECLARE MyUserCursor CURSOR
FOR
SELECT * FROM Users
WHERE useraddress IS NULL

In Oracle and POSTGRES, we use the IS keyword as below

DECLARE MyUserCursor CURSOR
IS
SELECT * FROM Users
WHERE useraddress IS NULL

Enjoyed this post? Share it!