IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Accessing DB2 tables using Rexx


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
diwa_thilak

Active User


Joined: 13 Jul 2006
Posts: 205
Location: At my desk

PostPosted: Thu Oct 11, 2007 12:55 pm
Reply with quote

Hi Friends,

I am planning to retrieve a single row from a table (DB2) using REXX utility.

Please throw some light about connecting DB2 tables using rexx and retrieving a data(row) from the table.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10886
Location: italy

PostPosted: Thu Oct 11, 2007 1:15 pm
Reply with quote

Start looking at

Quote:
DB2 UDB for OS/390 and z/OS V7 Application Programming and SQL Guide"


http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/SEARCH?SEARCHREQUEST=rexx&Book=DSNAPH16&DN=SC26-9933-06&DT=20061206114427&TYPE=FUZZY&RANK=RANK&SEARCHTOPIC=TOPIC&SEARCHTEXT=TEXT&SEARCHINDEX=INDEX&SHELF=DSNSHHA7.bks
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Thu Oct 11, 2007 4:16 pm
Reply with quote

You have to declare a cursor and fetch the row from the table, even if you know there will never be more than one row.

The format is:
To load DB2 and connect:
Code:
Address TSO "SUBCOM DSNREXX"                 
If RC <> 0 Then Do                           
   S_RC = RXSUBCOM('ADD','DSNREXX','DSNREXX')
   If S_RC <> 0 Then Do                     
      Say 'Problem Loading DB2 environment'
      Exit                                   
   End                                       
End
Address DSNREXX "CONNECT "DB2Name


To fetch the row:
Code:
SQLSTMT = "SELECT ... FROM ... WHERE ..."
Address DSNREXX                   
"EXECSQL DECLARE C7 CURSOR FOR S7"
"EXECSQL PREPARE S7 FROM :SQLSTMT"
"EXECSQL OPEN C7"                 
"EXECSQL FETCH C7 INTO :fld1, :fld2 ..."
LastSQL = SQLCODE
"EXECSQL CLOSE C7"                 
Address                           
Back to top
View user's profile Send private message
diwa_thilak

Active User


Joined: 13 Jul 2006
Posts: 205
Location: At my desk

PostPosted: Thu Oct 11, 2007 4:42 pm
Reply with quote

Thanks Marso,

I could able to connect to the DB2 successfully. But i could not able to retrieve the records.

I need to dynamically send values to the query.

SQLSTMT = "SELECT NUM_VAL_I FROM DSN.TABNAME WHERE ID_I = 045"

Also let me know whether :SQLSTMT , :FLD1 variables are accepted in REXX.

How to dynamically send values to the query ?
Back to top
View user's profile Send private message
diwa_thilak

Active User


Joined: 13 Jul 2006
Posts: 205
Location: At my desk

PostPosted: Thu Oct 11, 2007 4:58 pm
Reply with quote

Hi All,

Yeah :SQLSTMT and : FLD1 are valid statements in REXX.

But when i execute my below query i am getting SQLCODE as -981.

Quote:

-981 THE SQL STATEMENT FAILED BECAUSE THE RRSAF CONNECTION IS NOT IN A STATE THAT ALLOWS SQL OPERATIONS, REASON reason-code.


That means i could not able to connect to DB2. How to rebind and connect to DB2 ?
Back to top
View user's profile Send private message
ofer71

Global Moderator


Joined: 27 Dec 2005
Posts: 2358
Location: Israel

PostPosted: Thu Oct 11, 2007 10:56 pm
Reply with quote

The instructions regarding binding REXX to DB2 are well described in the fine manual.

O.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Sun Oct 14, 2007 1:09 pm
Reply with quote

Send values to the query like usual:
Code:
MyVal = 45
SQLSTMT = "SELECT NUM_VAL_I FROM DSN.TABNAME WHERE ID_I = "MyVal

If the variable is alphabetic, add quotes:
Code:
MyVal = "diwa"
SQLSTMT = "SELECT NUM_VAL_I FROM DSN.TABNAME WHERE ID_I = '"MyVal"'"

About -981, try to get the reason-code by displaying SQLERRMC:
Code:
If SQLCODE <> 0 Then Do
   Say "SQLCODE = "SQLCODE
   Say "SQLERRMC = "SQLERRMC
End
Back to top
View user's profile Send private message
diwa_thilak

Active User


Joined: 13 Jul 2006
Posts: 205
Location: At my desk

PostPosted: Tue Oct 23, 2007 1:08 pm
Reply with quote

Hi All,

I found the bug, The connection identifier to connect to the DB2 tables needs to be specified while connecting to the tables.

I missed to specify.

Thanks All Buddies (e.s, Marso, Ofer).
Back to top
View user's profile Send private message
lanand_hps

New User


Joined: 05 Dec 2007
Posts: 82
Location: chennai

PostPosted: Tue Dec 16, 2008 4:46 pm
Reply with quote

Hi All,
I have used similiar code in my REXX routine.
It is throwing a return code of 1 for open cursor.

My code :
SQLSTMT="SELECT CIM FROM DB2TEST.MASTER"
ADDRESS DSNREXX
"EXECSQL DECLARE C7 CURSOR FOR S7"
"EXECSQL PREPARE S7 FROM :SQLSTMT"
"EXECSQL OPEN C7"
"EXECSQL FETCH C7 INTO :FLD1"

I'm able to connect to DB2 subsystem.

Please comment.
Back to top
View user's profile Send private message
ofer71

Global Moderator


Joined: 27 Dec 2005
Posts: 2358
Location: Israel

PostPosted: Tue Dec 16, 2008 4:51 pm
Reply with quote

Take a look at Marso's post above.

O.
Back to top
View user's profile Send private message
lanand_hps

New User


Joined: 05 Dec 2007
Posts: 82
Location: chennai

PostPosted: Wed Dec 17, 2008 12:18 pm
Reply with quote

It is working with return code 1 (warning?).
Is it possible to connect to two subsystems in one rexx routine?

ADDRESS DSNREXX "CONNECT " DSN1
ADDRESS DSNREXX
"EXECSQL DECLARE C7 CURSOR FOR S7"
"EXECSQL PREPARE S7 FROM :SQLSTMT"
"EXECSQL OPEN C7"
"EXECSQL FETCH C7 INTO :FLD1

ADDRESS DSNREXX "CONNECT " DSN2
ADDRESS DSNREXX
"EXECSQL DECLARE C7 CURSOR FOR S7"
"EXECSQL PREPARE S7 FROM :SQLSTMT"
"EXECSQL OPEN C7"
"EXECSQL FETCH C7 INTO :FLD1

Data is present in DSN1 and not in DSN2.
I'm getting sqlcode(for fetch) 0 for both regions which is wrong.
Output of 2nd region is same as 1st.
Should i do anything different?
Back to top
View user's profile Send private message
ofer71

Global Moderator


Joined: 27 Dec 2005
Posts: 2358
Location: Israel

PostPosted: Wed Dec 17, 2008 12:35 pm
Reply with quote

Return code 1 for RXSUBCOM is not a warning. Read the manual.

O.
Back to top
View user's profile Send private message
lanand_hps

New User


Joined: 05 Dec 2007
Posts: 82
Location: chennai

PostPosted: Wed Dec 17, 2008 1:34 pm
Reply with quote

Ok. But can i have answer for my doubts?
Is it possible to connect to two subsystems in one rexx routine?
Back to top
View user's profile Send private message
saiprasadh

Active User


Joined: 20 Sep 2006
Posts: 154
Location: US

PostPosted: Wed Dec 24, 2008 4:46 am
Reply with quote

Hi,

Ya you can, But you have to disconnect the first Sub-System

For Ex: You have 2 sub systems Prod & Test

1) Connect to prod
2) Execute the query
3) Disconnect from prod
4) Connect to test
5) Execute the query
6) Disconnect from prod


Thanks
Sai
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Run rexx with JCL Job CLIST & REXX 1
No new posts Run rexx in batch job CLIST & REXX 7
No new posts Does anyone know rexx for VSE CLIST & REXX 3
No new posts TSO ALLOC In REXX Needs Improvement JCL & VSAM 3
No new posts REXX/CMS How to place command console... CLIST & REXX 4
Search our Forums:

Back to Top