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
  • 4
  • 2
  • 2
  • +2
LVL 18

Accepted Solution

daveslash 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.


Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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 100

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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