Executing a program in AS/400 using ODBC

Posted on 2011-09-28
Last Modified: 2012-05-12
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.
Question by:arturosm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
LVL 18

Accepted Solution

Dave Ford earned 250 total points
ID: 36815837

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

  EXTERNAL NAME MySchema/MyProgram

Open in new window

LVL 27

Expert Comment

ID: 36818113
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.


Author Comment

ID: 36881128
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.


Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 27

Expert Comment

ID: 36890049
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.

LVL 35

Expert Comment

by:Gary Patterson
ID: 36893740
The correct format is:


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:


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.

- Gary Patterson

LVL 27

Assisted Solution

tliotta earned 250 total points
ID: 36929013
No, the correct format is probably:


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:


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.

LVL 27

Expert Comment

ID: 36934520
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.


Author Comment

ID: 37004047

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

Thank you.
LVL 101

Expert Comment

ID: 37167283
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.
LVL 35

Expert Comment

by:Gary Patterson
ID: 37167284
Recommend you split points to 36929013 and 36815837.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question