• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

Executing a program in AS/400 using ODBC

Hello Experts.

Is it possible to execute an AS/400 program using ODBC driver from a Windows program? I have been told this is a program and NOT a stored procedure nor stored procedure wrap that launches the program.

I have read that the execution can be done using OLE DB or .NET provider but I want to know if it can be done using ODBC driver.

If it is possible to do that, it would be helpful a link or an small example.

Thank you in advance.
0
arturosm
Asked:
arturosm
  • 4
  • 2
  • 2
  • +2
2 Solutions
 
Dave FordSoftware Developer / Database AdministratorCommented:

I would probably be easiest to wrap the program as a stored procedure:

CREATE PROCEDURE MySchema/MyProgram
  LANGUAGE RPG 
  NOT DETERMINISTIC 
  MODIFIES SQL DATA
  EXTERNAL NAME MySchema/MyProgram
  PARAMETER STYLE GENERAL WITH NULLS

Open in new window

0
 
tliottaCommented:
If there all parameters are input only, you should be able call a program with ODBC just like a stored procedure. The DB2 server will execute a simple program call the same way it would execute a stored procedure call.

If any parm values need to be returned to the client, a full stored proc definition is required because the server needs to know how to present the returned values across the network.

A common example would be a call to the QCMDEXC API to execute a command.

Tom
0
 
arturosmAuthor Commented:
Hi tliotta:

Yes, there is only one input parameter, I tryed to call the program with call but got an exception, something like "the stored procedure has been prepared". I'm not sure about the syntax of the call, I'm using: "CALL LIBRARY.PROGRAMNAME(CHARPARAM)".

---------------------------------------------

Thank you daveslash.

I have to call this program as part of another not related project, so the people in charge of the AS400 will not even do that simple stored procedure. I think I can't create it with the user I have, and my knowledge of AS400 is almost null, I said "almost" so I don't make myself feel bad. I will try to create it using the text in a SQL statement.

-----------------

Regards.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
tliottaCommented:
There are the CHAR and the VARCHAR parameter types possible in this type of case. Without a declaration, it takes some care to ensure that any value that you supply gets across the network so that the database server knows how to present the value to the target program.

Do you know what how target program declares the variable? How is CHARPARAM actually supplied in the client? I.e., what is the exact statement in the client? And the actual exception will probably be needed.

Tom
0
 
Gary PattersonVP Technology / Senior Consultant Commented:
The correct format is:

CALL LIBRARY/PROGRAMNAME PARM('CHARPARAM')

If passing this string directly doesn't work, you'll need to wrap it in a call to the system stored procedure QCMDEXEC (whci in turn is just a wrapper for the system API QCMDEXC:

CALL QSYS.QCMDEXC('CALL LIBRARY/PROGRAMNAME PARM(''CHARPARAM'')',   0000000044.00000)
-or-
CALL QSYS/QCMDEXC('CALL LIBRARY/PROGRAMNAME PARM(''CHARPARAM'')',   0000000044.00000)

Depending on the SQL naming format used by the ODBC connection (*SQL =".", and *SYS ="/").   Note that this only applies to SQL commands and not system commands.  Confusing, since the leftmost CALL is a SQL CALL command, and the rightmost CALL is an OS CALL command.

Note that all of the quotes are 'single' quotes, including the doubled single quotes around CHARPARAM.  The "44" is the length of the system command in bytes.  This value must be padded out to 10 digits on the left of the decimal and five digits to the right.

Note that this technique can be used not only to execute programs using the OS "CALL" command, but can be used to execute any OS command.

http://www.itjungle.com/mgo/mgo080803-story01.html

- Gary Patterson



0
 
tliottaCommented:
No, the correct format is probably:

CALL LIBRARY.PROGRAMNAME('CHARPARAM')

But it depends on the declaration of CHARPARAM in program PROGRAMNAME.

If it is declared as a CHAR(9), then the above CALL should work correctly. But if it's defined as a CHAR(10), then there needs to be a blank at the end:

CALL LIBRARY.PROGRAMNAME('CHARPARAM ')

In short, the value of CHARPARAM can be passed unquoted if it is defined as a VARCHAR type. But if it is a CHAR type, it must be passed as the declared length quoted and padded with blanks or truncated, whichever gives the exact length. A CHAR(20) would require eleven trailing blanks all within quotes.

There is no need to call PROGRAMNAME through any intermediate stored proc. There is no need for a wrapper for any program that only has inbound parms -- including QCMDEXC.

That's always been true for DB2 on AS/400s.

Tom
0
 
tliottaCommented:
Note that "CHARPARAM" would refer to the value represented rather than a variable in the client when quoted. The value must be resolved and quoted as the appropriate length when it is declared as a character data type. A variable-length character data type wouldn't require quotes, nor is there an actual declared length in the called program (i.e., it's "variable").

Without quotes, most clients will structure the request to the remote database so that the DBMS will go looking for a signature match that has a VARCHAR parm. That's not a common case when calling an undeclared stored proc. Most bare program objects will declare fixed-length character parms.

Tom
0
 
arturosmAuthor Commented:
Hello.

Next Monday I will go to the site to try your solutions.

Thank you.
0
 
mlmccCommented:
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
Gary PattersonVP Technology / Senior Consultant Commented:
Recommend you split points to 36929013 and 36815837.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now