如何在as400上以編程方式運行復雜查詢?

[英]How do I programmatically run a complex query on an as400?


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.

我是as400的新手,我有一個查詢4個表的連接。查詢本身很好,它在STRSQL中運行並顯示結果。

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

我正在努力的是讓查詢能夠以編程方式運行(它最終將從計划的CL腳本運行)。

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.

我嘗試創建一個物理文件,其中包含使用RUNQRY運行它的查詢,但它只顯示查詢本身,而不是實際結果集。

Does anyone know what I am doing wrong?

有誰知道我做錯了什么?


UPDATE

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):

感謝大家的方向和資源,我們能夠實現我的目標。如果它可以幫助任何人,這就是我最終做的事情(所有這些都是在它自己的庫中完成的,ALLOCATE):

  1. Created a source physical file (using CRTSRCPF): QSQLSRC, and created a member named SQLLEAGSEA, with the type of TXT, that contains the SQL statement.

    創建了一個源物理文件(使用CRTSRCPF):QSQLSRC,並創建了一個名為SQLLEAGSEA的成員,其類型為TXT,其中包含SQL語句。

  2. 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:

    創建了另一個源物理文件:QCLSRC,並創建了一個名為POPLEAGSEA的成員,其類型為CLP,它將當前庫更改為ALLOCATE,然后使用RUNSQLSTM運行查詢(下面有更多詳細信息)。這是實際的命令:

    RUNSQLSTM SRCFILE(QSQLSRC) SRCMBR(SQLLEAGSEA) COMMIT(*NONE) NAMING(*SYS)

    RUNSQLSTM SRCFILE(QSQLSRC)SRCMBR(SQLLEAGSEA)COMMIT(* NONE)命名(* SYS)

  3. Added the CLP to the scheduled jobs (using ADDJOBSCDE), running the following command:

    將CLP添加到計划作業(使用ADDJOBSCDE),運行以下命令:

CALL PGM(ALLOCATE/POPLEAGSEA)

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:

關於RUNSQLSTM,我的研究表明我不能使用這個函數,因為它不支持SELECT語句。我沒有在我的問題中指出的是我需要對結果做些什么 - 我將把結果數據插入到另一個表中(如果我這樣做了,我確信幫助可能已經解決了很多問題)更快)。如此有效,我不會做一個SELECT,我的最終結果實際上是一個INSERT。所以我的SQL語句(在SQLLEAGSEA中)以:

INSERT INTO ALLOCATE/LEAGSEAS

插入ALLOCATE / LEAGSEAS

SELECT ... BLAH BLAH BLAH ...

選擇...... BLAH BLAH BLAH ......

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.

根據我的研究,我認為RUNSQLSTM不支持SELECT,因為它沒有機制對結果做任何事情。一旦我停止采取嬰兒步驟並意識到我需要在同一語句中選擇和插入,它解決了我的主要問題。

Thanks again everyone!

再次感謝大家!

6 个解决方案

#1


4  

The command is RUNSQLSTM to run a static SQL statement in a physical file member or stream file.

該命令是RUNSQLSTM,用於在物理文件成員或流文件中運行靜態SQL語句。

It is a non-interactive command so it will not execute sql statements that attempt to return a result set.

它是一個非交互式命令,因此它不會執行嘗試返回結果集的sql語句。

If you want more control, including the ability to run interactive statements, see the Qshell db2 utility.

如果您想要更多控制,包括運行交互式語句的能力,請參閱Qshell db2實用程序。

For example:

QSH CMD('db2 -f /QSYS.LIB/MYLIB.LIB/MYSRCFILE.FILE/MYSQL.MBR')

Note that the db2 utility only accepts the *SQL naming convention.

請注意,db2實用程序僅接受* SQL命名約定。

#2


4  

QM Query

If all the SQL you need is the single complex SQL statement, and this is what it sounds like, then your best bet is to use Query Management Query (see QM Query manual here).

如果您需要的所有SQL都是單個復雜的SQL語句,這聽起來就是這樣,那么您最好的選擇是使用查詢管理查詢(請參閱此處的QM查詢手冊)。

The results can be directed to a display, a spool file, or a physical file (ie a DB2 table). The default output when run interactively is to the screen, but when run in a (scheduled) batch job it will default to a spool file report.

結果可以指向顯示,假脫機文件或物理文件(即DB2表)。交互式運行時的默認輸出是到屏幕,但是當在(計划的)批處理作業中運行時,它將默認為假脫機文件報告。

You can create the QM Query interactively via WRKQMQRY, in prompted mode (much like Query/400) or in SQL mode. Or you can compile the QM Query from source, with the CRTQMQRY command.
To run your QM Query, STRQMQRY command.

您可以通過WRKQMQRY以提示模式(非常類似於Query / 400)或SQL模式以交互方式創建QM查詢。或者,您可以使用CRTQMQRY命令從源編譯QM查詢。要運行QM查詢,請執行STRQMQRY命令。

RUNSQL cmd

If you are using a system that has IBM i 7.1 fully up-to-date, and has Technology Refresh 4 (TR4) installed, then you could also use the new RUNSQL command to execute a single statement. (see discussion in developerWorks)

如果您使用的系統IBM i 7.1完全是最新的,並且安裝了Technology Refresh 4(TR4),那么您還可以使用新的RUNSQL命令來執行單個語句。 (參見developerWorks中的討論)

SQL Scripting w/ RUNSQLSTM cmd

From CL you can run SQL scripts of multiple SQL statements from a source file member. There is no standard default source file name for this, but QSQLSRC is commonly used. The source member can contain multiple non-interactive SQL statements. This means you cannot use a SELECT statement (directly) since theoretically it will not know where to send the results. CL commands are even allowed if given a CL: prefix. Both SQL and CL statements should be terminated with a semicolon ;. While the SQL statements cannot display data directly to the screen, the same restriction does not apply to the scripted CL commands.

從CL,您可以從源文件成員運行多個SQL語句的SQL腳本。沒有標准的默認源文件名,但通常使用QSQLSRC。源成員可以包含多個非交互式SQL語句。這意味着您不能(直接)使用SELECT語句,因為理論上它不知道將結果發送到何處。如果給出CL:前綴,甚至允許CL命令。 SQL和CL語句都應以分號結束;雖然SQL語句無法直接向屏幕顯示數據,但相同的限制不適用於腳本化CL命令。

The STRQMQRY command can be embedded in the RUNSQLSTM script, by placing the prefix "CL: " in front of the command. Since STRQMQRY can direct output to the screen, a report, or an output table, this can come in very useful.

通過在命令前放置前綴“CL:”,可以將STRQMQRY命令嵌入到RUNSQLSTM腳本中。由於STRQMQRY可以將輸出定向到屏幕,報表或輸出表,因此這非常有用。

Remember that to direct your output from a SELECT query to a file you can use either the INSERT or CREATE TABLE statements.

請記住,要將SELECT查詢的輸出定向到文件,可以使用INSERT或CREATE TABLE語句。

CREATE TABLE newtbl AS
  ( full-select )
  WITH DATA;

Or, to put the results into a table you create in your job's QTEMP library:

或者,將結果放入您在作業的QTEMP庫中創建的表中:

DECLARE GLOBAL TEMPORARY TABLE AS
  ( full-select )
  WITH DATA;

[Note: If you create the source to be used by CRTQMQRY, you are advised to create it as CRTSRCPF yourlib/QQMQRYSRC RCDLEN(91), since the compiler will only use 79 columns of your source data (adding 12 for sequence and change date =91). However for QM Forms, which can be used to provide additional formatting, the CRTQMFORM compiler will use 81 columns so RCDLEN(93) is advised for QQMFORMSRC.]

[注意:如果您創建CRTQMQRY使用的源,建議您將其創建為CRTSRCPF yourlib / QQMQRYSRC RCDLEN(91),因為編譯器將只使用79列源數據(為序列和更改日期添加12列) = 91)。但是對於可以用來提供額外格式的QM Forms,CRTQMFORM編譯器將使用81列,因此建議將QQ網絡用於QQMFORMSRC。

#3


2  

RUNQRY is a utility that lets you execute a query that was created by another utility named WRKQRY. If you really want to process SQL statements held in a file try RUNSQLSTM. It uses a source physical file to store the statements, not a database file. The standard name for that source physical file is QQMQRYSRC. To create that file, CRTSRCPF yourlib/QQMQRYSRC. Then you can use PDM to work with that source PF. WRKMBRPDM yourlib/QQMQRYSRC. Use F6 to create a new source member. Make it source type TXT. Then use option 2 to will start an editor called SEU. Copy/paste your SQL statements into this editor. F3 to save the source. Once the source is saved, use RUNSQLSTM to execute it.

RUNQRY是一個實用程序,它允許您執行由另一個名為WRKQRY的實用程序創建的查詢。如果你真的想處理文件中保存的SQL語句,請嘗試RUNSQLSTM。它使用源物理文件來存儲語句,而不是數據庫文件。 QQ源物理文件的標准名稱是QQMQRYSRC。要創建該文件,請使用CRTSRCPF yourlib / QQMQRYSRC。然后,您可以使用PDM來處理該源PF。 WRKMBRPDM yourlib / QQMQRYSRC。使用F6創建新的源成員。使其成為源類型TXT。然后使用選項2啟動一個名為SEU的編輯器。將SQL語句復制/粘貼到此編輯器中。 F3保存源。保存源后,使用RUNSQLSTM執行它。

#4


2  

It is (now) possible to run SQL directly in a CL program without using QM Query, RUNSQLSTM or QShell.

現在可以在不使用QM Query,RUNSQLSTM或QShell的情況下直接在CL程序中運行SQL。

Here is an article that discusses the RUNSQL statement in CL programs...

這篇文章討論了CL程序中的RUNSQL語句......

http://www.mcpressonline.com/cl/the-cl-corner-introducing-the-new-run-sql-command.html

The article contains information on what OS levels are supported as well as clear examples of several ways to use the RUNSQL statement.

本文包含有關支持哪些操作系統級別的信息,以及使用RUNSQL語句的幾種方法的清晰示例。

#5


1  

This will work in two steps:

這將分兩步進行:

 RUNSQL SQL('CREATE TABLE QTEMP/REPORT AS (SELECT +         
          EXTRACT_DATE , SYSTEM, ODLBNM, SUM( +              
          OBJSIZE_MB ) AS LIB_SIZE FROM +                    
          ZSYSCOM/DISKRPTHST WHERE ODLBNM LIKE +             
          ''SIS%'' GROUP BY EXTRACT_DATE, SYSTEM, +          
          ODLBNM ORDER BY LIB_SIZE DESC) WITH +              
          DATA') COMMIT(*NONE) DATFMT(*USA) DATSEP(/)        

 RUNQRY     QRYFILE((QTEMP/REPORT)) OUTTYPE(*PRINTER) +          
          OUTFORM(*DETAIL) PRTDFN(*NO) PRTDEV(*PRINT)        

The first step creates a temporary table result in qtemp and the second step/line runs an adhoc query over just the temporary table to a spool file.

第一步在qtemp中創建臨時表結果,第二步/行僅在臨時表上運行臨時表查詢到假脫機文件。

Thanks, Michael Frilot

謝謝Michael Frilot

#6


1  

There is of course a totally different solution: You could write and compile a program containing the statement. It requires some longer reading into, especially if you are new to the platform, but it should give you most flexibility over what you do with results. You can use SQL in C, C++, RPG, RPG/LE, REXX, PL (of which I don't know, what it is) and COBOL. Doing that, you can react in any processable way on results from one query and start/create other queries based on what you get.

當然有一個完全不同的解決方案:您可以編寫和編譯包含該語句的程序。它需要更長時間的閱讀,特別是如果您是平台的新手,但它應該為您提供最大的靈活性,而不是您對結果的處理。您可以在C,C ++,RPG,RPG / LE,REXX,PL(我不知道,它是什么)和COBOL中使用SQL。這樣做,您可以以任何可處理的方式對一個查詢的結果做出反應,並根據您獲得的內容啟動/創建其他查詢。

Although some oldfashioned RPG-programmers try everything to deny SQL in RPG exists, it is possible today for many cases, to write RPG-programs with SQL only and no direct file access (without F-Specs, for those who know RPG).

雖然一些老式的RPG程序員嘗試拒絕在RPG中存在SQL,但在很多情況下,有可能只用SQL編寫RPG程序而沒有直接文件訪問(沒有F-Specs,對於那些了解RPG的人)。

If your solution works for you, perfect. If you need to do something else, try a look into this pdf: http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/rzajp/rzajp.pdf

如果您的解決方案適合您,那就完美了。如果您需要做其他事情,請查看此pdf:http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/rzajp/rzajp.pdf

The integration into RPG is not too bad. It works with the normal program flow. Would look something like this (in free form):

與RPG的整合也不算太糟糕。它適用於正常的程序流程。看起來像這樣(自由形式):

/free
    // init search values:
    searchval = 'Someguy';
    // so the sql query:
    exec sql
      SELECT colum1, colum2
      INTO :var1, :var2
      FROM somelib/somefile
      WHERE keycol=:searchval;
    // now do something with the values:
    some_proc(var1);
/end-free

In this, var1, var2, and searchval are ordinary RPG-variables. No quoting needed. Works also with datastructures (externally defined e.g., the record format of the file itself fits well). You can work with cursors and loops, too, of course. I feel that RPG-programs tend to be easier to read with this.

在這里,var1,var2和searchval是普通的RPG變量。不需要報價。也適用於數據結構(外部定義,例如,文件本身的記錄格式很合適)。當然,你也可以使用游標和循環。我覺得RPG程序往往更容易閱讀。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/16/731668b3f8ac7b118cb2dc7e15d3d9d4.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com