title: How do I programmatically run a complex query on an as400?
body: <p>I'm new at working on an as400 and I have a query the joins across 4 tables. The query itself is fine, it runs in STRSQL and displays the results.</p>
  <p>What I am in struggling with is getting the query to be able to run programmatically (it will eventually be run from a scheduled CL script).</p>
  <p>I tried have creating a physical file that contains the query running it with RUNQRY, but it simply displays the query itself, not the actual result set.</p>
  <p>Does anyone know what I am doing wrong?</p>
  <hr />
  <h2>UPDATE</h2>
  <p>Thanks everyone for the direction and the resources, with them I was able to reach my goal. In case it helps anyone, this is what I ended up doing (all of this was done in it's own library, ALLOCATE):</p>
  <ol>
  <li><p>Created a source physical file (using CRTSRCPF): QSQLSRC, and created a member named SQLLEAGSEA, with the type of TXT, that contains the SQL statement.</p>
  </li>
  <li><p>Created another source physical file: QCLSRC, and created a member named POPLEAGSEA, with the type of CLP, that changes the current library to ALLOCATE then runs the query using RUNSQLSTM (more detail on this below). Here is the actual command:</p>
  <p>RUNSQLSTM SRCFILE(QSQLSRC) SRCMBR(SQLLEAGSEA) COMMIT(*NONE) NAMING(*SYS)</p>
  </li>
  <li><p>Added the CLP to the scheduled jobs (using ADDJOBSCDE), running the following command:</p>
  </li>
  </ol>
  <p>CALL PGM(ALLOCATE/POPLEAGSEA)</p>
  <p>With regard to RUNSQLSTM, my research indicated that I wasn't going to be able to use this function, because it didn't support SELECT statements. What I didn't indicate in my question was what I needed to do with the the result - I was going to be inserting the resultant data into another table (had I done that I'm sure the help could have figured that out a lot quicker). So effectively, I wasn't going to be doing an SELECT, my end result is actually an INSERT. So my SQL statement (in SQLLEAGSEA) begins with:</p>
  <p>INSERT INTO
  ALLOCATE/LEAGSEAS</p>
  <p>SELECT
  ...
  BLAH BLAH BLAH
  ...</p>
  <p>From my research, I gather that RUNSQLSTM doesn't support SELECT because it doesn't have a mechanism to do anything with the results. Once I stopped taking baby steps and realized I needed to SELECT AND INSERT in the same statement, it solved my main problem.</p>
  <p>Thanks again everyone!</p>