Learn how to use host variables to get the most from your embedded SQL statements.
In its simplest form, embedded SQL in RPGLE is not much different from the type of SQL you can run using the RUNSQLSTM command. You can use DDL to create and drop tables; you can even insert and delete rows. However, you will soon find that you will want to have the SQL statement affect different rows, depending on user input or other criteria. That's where host variables can help.
Host Variables
Host variables are variables used within the SQL statement that correspond to variables defined in your program. They are denoted with a colon. The IBM Redbook Modernizing IBM eServer iSeries Data Access - A Roadmap Cornerstone notes some important rules about naming these host variables:
• Host variables should not begin with SQ, SQL, RDI, or DSN because the SQL precompiler creates variables with these prefixes.
• Variable names are limited to 4096 characters in RPG.
• The name of the host variable should be unique within the source member. For example, if there is a global variable "foo" and a variable "foo" within a subprocedure, "foo" should not be used as a host variable. Subfields of a qualified DS that have the same name as another field may be used. They must be specified in their qualified form.
• Array elements cannot be used as host variables.
Using a Host Variable
Suppose we have an application in which users can view orders and add notes for others who may deal with the order. Simplistically, we might have a file with two fields, orderNumber and comment.
We could use a host variable to delete all of the comments for the current order:
C/EXEC SQL
C+ DELETE
C+ FROM ordCmmts
C+ WHERE orderNumber = :currentOrder
C/END-EXEC
The variable currentOrder would be declared in your RPG program and would contain the current order number.
We can also receive the results of an SQL query into host variables. Following the earlier example, we might want to ask the user to confirm the deletion of the comments. We say, "Are you sure you want to delete all 20 comments from this order?" We can find out the number of comments for the order with this handy SQL query:
C/EXEC SQL
C+ SELECT count(*) INTO :deleteCount
C+ FROM ordCmmts
C+ WHERE orderNumber = :currentOrder
C/END-EXEC
Now the variable deleteCount holds the number of comments for the current order.
Host variables aren't restricted only to the WHERE clause. They can be used in the INSERT clause to write a record to the table. They can be used in the SET clause to change the value of fields in an existing record. We can also use them as literals in the SELECT clause. For example, the following statement would put the comments for the current order into another file, and the contents of the variable user would fill the first field of the file. I mention this specifically because you can't use host variables to change which fields are selected from a file. That requires the use of dynamic SQL.
C/EXEC SQL
C+ INSERT INTO otherCmmts
C+ SELECT :user, orderNumber, comment
C+ FROM ordCmmts
C+ WHERE orderNumber = :currentOrder
C/END-EXEC
This is a fairly simple introduction to using host variables in RPGLE embedded SQL. Data structures and data structure arrays can also be used as host variables. There are certain cases where host variables will not work, such as within the IN clause, as in 'SELECT * FROM table WHERE date IN (date1,date2,...,dateN)'. This query is an example of dynamic SQL. For more information about using embedded SQL in RPG, I found chapter 7 of the IBM Redbook Modernizing IBM eServer iSeries Data Access - A Roadmap Cornerstone to be very informative.
LATEST COMMENTS
MC Press Online