5. Embedded QUEL : Retrieve Statement : How the Sort Clause Works
 
Share this page                  
How the Sort Clause Works
The sort clause is used to sort result rows based on the contents of one or more columns. The names of result columns in the EQUEL retrieve statement are also names of program variables (the variables that receive the data from the retrieve). When coding the sort clause, you must typically dereference the sort column names.
For example:
begin program
## ename  character_string(26)
## eno    integer
## salary float
## ingres "personnel"
## range of e is emp
## retrieve (eno = e.empnum, ename = e.empname, 
##  salary = e.#salary)
## sort by #eno
## {
     print eno, ename, salary
## }
## exit
end program
In this example, the sort column in the sort by clause must be dereferenced to sort on the "eno" column. If the column were not dereferenced, EQUEL assumes that the variable "eno" contained the name of the sort by column.
In the following example, the application prompts the user for the desired sort column; the user-specified sort key is read into the "sort_key" variable, which is used in the sort by clause. In this example, the variable must not be dereferenced: it is a variable and not a column name.
begin program
## ename    character_string(26)
## sort_key character_string(24)
## eno      integer
## salary   float
## ingres "personnel"
   print "Select sort column to use for employee list;"
   print "choices are eno, ename, or salary: "
   read sort_key from terminal
## range of e is emp
## retrieve (eno = e.empnum, ename = e.empname, 
##   salary = e.#salary)
## sort by sort_key
## {
   print eno, ename, salary
## }
## exit
end program