Wednesday, August 10, 2016

Accessing DB2 from REXX: DSNREXX

We shall access DB2 from REXX using DSNREXX. This may sound easy but in REXX the error messages are very rarely could be interpreted to the code error and corrected. Hence, it is important that all the steps given here are followed as described.

It necessary to add the steplib ddname in the REXX where you wish to connect and work in DB2.

"STEPLIB ADD DA('<Db2 Subsystem>.DBMS.LNK',                                         'SYSC.<Db2 Subsystem>.SDSNLOAD') nomsg"                            
Db2 Subsystem value be according to your DB2 installation in your organization. 
Please note that these libraries are necessary to working with DB2 in REXX.

ADDRESS DSNREXX  'CONNECT' SUBSYS                                       

SUBSYS is a variable which contains your Db2 Subsytem name. 

SQLSTMT ="SELECT * FROM " DBNAME"."TABLENAME 

here DBNAME is a variable which contains your Table Qualifier
and TABLENAME is a variable which contains your Table Name.

ADDRESS DSNREXX
"EXECSQL PREPARE S1 FROM :SQLSTMT"                                     
"EXECSQL DECLARE C1 CURSOR FOR S1"                                     
"EXECSQL OPEN C1"                                                      

 "EXECSQL FETCH C1 INTO :Your-Variable"

Your-Variable will contain the value after the execution of the SQL statement. 

"EXECSQL CLOSE C1"

"STEPLIB REMOVE DA('<DB2 Subsystem>.DBMS.LNK',
                                           'SYSC.<DB2 Subsystem>.SDSNLOAD') nomsg"

Please use the double quotes(" ") and single quotes(' ') as given above. In case of queries, please feel free to ask in the comments.

No comments:

Post a Comment

Featured Post

REXX Skeleton: Submitting jobs through Rexx

Submitting jobs through REXX and reading the spool through REXX gives us immense potential to automate many manual activities in mainframes....