In the COBOL+DB2 Application, you can use either Singleton SELECT statement or DB2 Cursors to fetch data from the DB2 table. To process fetch data in your COBOL+DB2 Application, you require host variables. In this article, you’ll learn what are Host Variables in the COBOL+DB2 application? and How do you declare a host variable in COBOL programs?
Let's get started with Host variables in DB2.
Agenda.
- Introduction.
- What are host variables in COBOL+DB2 Applications?
- How do you declare a host variable in COBOL+DB2 Applications?
- Host Variable in DB2 Examples.
- How to use host variable in SQL statements?
- Conclusion.
Introduction.
The working principle of host programming languages (i.e. COBOL, JAVA, REXX etc.) is different from SQL. Host programming language is capable of processing a single record at a time, but SQL process a set of records. To bridge the gap, IBM introduced the concept of the DB2 cursor.
You can use either a singleton SELECT statement or a DB2 cursor to process data from the result set. To process fetch data in your COBOL+DB2 Application, you require host variables.
What are host variables in COBOL+DB2 Applications?
In laymen terms, host variables are areas of storage provided by the host language (i.e. COBOL, JAVA, C++, etc.) and referred to in SQL statements. You can name and define host variables using the host language's syntax.
If you are writing a COBOL program, then host variables must be defined in the DATA DIVISION of your program under the WORKING-STORAGE section or the LINKAGE section.
It is necessary that the host variables in the host language and the DB2 table column have compatible data types. However, It is not possible to identify SQL objects, such as tables or views, by using host variables, except when describing a table.
How do you declare a host variable in COBOL+DB2 Applications?
All host variables must be declared with the host program. COBOL+DB2 application makes use of host variables to:
- The application program should retrieve the data and store it in the host variables.
- Data can be inserted into a table or updated by using variables stored in the host.
- Use the data in the variable as input to a WHERE or HAVING clause.
The naming conventions of host variables are determined by the host language. To tell DB2 that a host variable is not a column name, a colon (:) must be used before the variable name in SQL. A colon is not allowed before host variables outside of SQL statements.
Host Variable in DB2 Example.
In SQL statements, host variables are commonly used as follows:
MOVE '987365' TO HEMPID.
EXEC SQL
SELECT FNAME, LASTNAME
INTO :HFNAME,
:HLNAME
FROM TSD001.EMPLOYEE
WHERE EMPNO = :HEMPID
END-EXEC.
Note: In the above example, HFNAME, HNAME, HEMPID and HEMPID are host variables defines in the COBOL+DB2 application program.
How to use host variable in SQL statements?
These are the most common ways to use host variables within SQL statements:
- WHERE clause: Host variables can be used to specify values as predicates or to replace literal values.
- INTO clause: A host variable can be used to specify the data area in which the column values from a retrieved row will be stored. You can use the INTO clause to specify one or more host variables in which column values are to be stored.
- SELECT clause: You can specify a list of items that do not have to match the column names of tables or views in the SELECT clause. A program can return column values, host variables, and literal constants.
Conclusion.
Finally, this concludes our Host variable in COBOL or Host Variable in DB2 or simply Host Variable. In this article, we will look at what are host variables?, How to define a host variable in COBOL+DB2 applications, and DB2 host variables examples. Do check out COBOL LEVEL 88 Condition.
►Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on -
► Youtube
► Facebook
► Linkedin
► Reddit
Thank you for your support.
Mainframe Forum™
No comments:
Post a Comment