In the previous lesson, you may have noticed that the delete statement included the keyword noCursor. Whereas the get statement selects one or more rows from an entire table, the delete statement normally operates on a row indicated by a cursor that was created by a previous statement. The noCursor keyword indicates that no such cursor exists and that the statement should use a WHERE clause to identify the row in the same way as the default get statement.
The EGL open statement creates a cursor and indicates a result set, or a set of one or more rows that the cursor can move through. Instead of accessing the cursor directly, you use other EGL statements to retrieve the next row from the result set as indicated by the cursor, or perform operations on the entire result set, using the cursor to step through the rows one at a time.
tempItem Item; open allExpensiveItems for tempItem;In this example, tempItem is a single record variable from the SQLRecord part representing the ITEMS table of the database. The code for tempItem specifies that EGL will use the cursor to move rows into this record one at a time. The code open allExpensiveItems gives the result set a name; allExpensiveItems is an identifier, not a variable, strictly speaking.
tempItem Item;
open expensiveItems for tempItem with
#sql{
select
EGL.ITEM.ITEM_ID, EGL.ITEM.NAME, EGL.ITEM.IMAGE,
EGL.ITEM.PRICE, EGL.ITEM.DESCRIPTION
from EGL.ITEM
where
EGL.ITEM.ITEM_ID >= :tempItem.ItemId
order by
EGL.ITEM.ITEM_ID asc
};
This default WHERE clause specifies only that each item must
have an ITEM_ID value higher than the one before. (EGL manages the SQL DECLARE
CURSOR and OPEN CURSOR statements and does not show them in the explicit SQL.)tempItem Item;
maxCost Price = 500;
open expensiveItems for tempItem with
#sql{
select
EGL.ITEM.ITEM_ID, EGL.ITEM.NAME, EGL.ITEM.IMAGE,
EGL.ITEM.PRICE, EGL.ITEM.DESCRIPTION
from EGL.ITEM
where
EGL.ITEM.PRICE >= :maxCost
order by
EGL.ITEM.ITEM_ID asc
};
Now the expensiveItems result set contains all of the rows
from the database with a PRICE column greater than or equal to the maxCost
variable, which is set to 500.tempItem Item;
maxCost Price = 500;
open expensiveItems for tempItem with
#sql{
select
EGL.ITEM.ITEM_ID, EGL.ITEM.NAME, EGL.ITEM.IMAGE,
EGL.ITEM.PRICE, EGL.ITEM.DESCRIPTION
from EGL.ITEM
where
EGL.ITEM.PRICE >= :maxCost
order by
EGL.ITEM.ITEM_ID asc
};
foreach (tempItem)
SysLib.writeStdout(tempItem.Name :: " " ::
tempItem.Description :: " $" :: tempItem.Price);
end
tempItem Item;
maxCost Price = 500;
open expensiveItems for tempItem with
#sql{
select
EGL.ITEM.ITEM_ID, EGL.ITEM.NAME, EGL.ITEM.IMAGE,
EGL.ITEM.PRICE, EGL.ITEM.DESCRIPTION
from EGL.ITEM
where
EGL.ITEM.PRICE >= :maxCost
order by
EGL.ITEM.ITEM_ID asc
};
// Retrieve the first row.
get next tempItem;
// As long as there are more rows,
// move the next row into the temporary variable
// and print its values.
while (sysvar.sqlData.sqlcode == 0)
SysLib.writeStdout(tempItem.Name :: " " ::
tempItem.Description :: " $" :: tempItem.Price);
get next tempItem;
end
The get statement has a variety
of other operations that you can use with an open cursor and result set:open expensiveItems scroll for tempItem;
close expensiveItems;Once the cursor and result set are closed, get next and other statements that depend on the cursor no longer work.
Laptop PC Great little machine. Take it anywhere with you. $1111.11 Combination Safe Keep your valuables safe and secure. Put 'em in here. $222.22 Desktop PC Lots'a power, at the right price. Includes monitor. $888.55
Here is the complete code of the selectItems.egl file. If you see any errors marked by red X symbols in the file, make sure your code matches the code in this file:Completed selectItems.egl file after lesson 3.