How Many Rows Did SQL Fetch?

Published: March 29, 2006

Hey, Ted:
I am using SQL to load a subfile. Since there are 16 subfile records to a page, I fetch 16 database records into a data structure. However, the fetch does not always return 16 rows. Sometimes it returns fewer. How can I tell how many data structure occurrences were fetched? --Lynn

You will need to check the SQLER3 field of the SQL Communications Area (SQLCA). I wrote about this field earlier in Four Hundred Guru. See "How Many Records Did SQL Delete?" Here's an example I threw together that you can start from. It fetches seven records at a time from file QCUSTCDT, which you should be able to find in library QIWS on your system.
Fqsysprt D D D D CustRec RecCt Index FetchCt o f 132 printer extname(QCUSTCDT) occurs(7) inz 3p 0 10i 0 3p 0

e ds s s s

C eval *inlr = *on C/exec sql C+ declare c1 cursor for C+ select * C+ from qcustcdt C/end-exec C/exec sql C+ open c1 C/end-exec C dow '1' C/exec sql C+ fetch c1 C+ for 7 rows C+ into :CustRec C/end-exec C if sqlstt <> *zeros C leave C endif C eval FetchCt += 1 C for index = 1 to SqlEr3 C eval RecCt += 1 C index occur CustRec C except pline C endfor C enddo C/exec sql C+ close c1 C/end-exec C return Oqsysprt e pline 1 O FetchCt 4

the statement will be accepted. If the source program specifies INCLUDE SQLCA.O O O O O RecCt SQLEr3 cusnum lstnam init 4 +0001 +0001 +0001 +0001 +0001 The program has two loops. The remaining columns come from the database file. The first column counts the fetches. as defined for ILE RPG for AS/400: D* SQL Communications area D SQLCA DS D SQLAID 1 8A D SQLABC 9 12B D SQLCOD 13 16B D SQLERL 17 18B D SQLERM 19 88A D SQLERP 89 96A D SQLERRD 97 120B D SQLERR 97 120A D SQLER1 97 100B D SQLER2 101 104B D SQLER3 105 108B D SQLER4 109 112B D SQLER5 113 116B D SQLER6 117 120B 0 0 0 0 DIM(6) 0 0 0 no of records fetched by cursor 0 0 0 . The third column is SQLER3. whereas the second fetch retrieved five. The second column numbers the records. The SQLCA. The outer loop fetches up to seven rows into a multiple-occurrence data structure. The inner loop processes the data that was placed into the data structure. Notice that the program had to fetch twice to access 12 rows. It shows that the first fetch retrieved seven rows. but it is redundant. Here's the output I got when I ran the program. Notice that the SQLER3 field serves to limit the number of iterations of the inner loop. 1 1 1 1 1 1 1 2 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12 000000007 000000007 000000007 000000007 000000007 000000007 000000007 000000005 000000005 000000005 000000005 000000005 938472 839283 392859 938485 397267 389572 846283 475938 693829 593029 192837 583990 Henning Jones Vine Johnson Tyron Stevens Alison Doe Thomas Williams Lee Abraham G B S J W K J J A E F M K D S A E L S W N D L T --Ted The SQL precompiler automatically places the SQLCA in the definition specifications of the ILE RPG for AS/400 program prior to the first calculation specification. INCLUDE SQLCA should not be coded in the source program.

How Many Records Did SQL Delete? Hey.D SQLWRN D SQLWN0 D SQLWN1 D SQLWN2 D SQLWN3 D SQLWN4 D SQLWN5 D SQLWN6 D SQLWN7 D SQLWN8 D SQLWN9 D SQLWNA D SQLSTT D* End of SQLCA 121 121 122 123 124 125 126 127 128 129 130 131 132 131A 121A 122A 123A 124A 125A 126A 127A 128A 129A 130A 131A 136A Note: Variable names in RPG for AS/400 are limited to 6 characters. Is it possible for my program to determine how many records are deleted when the statement executes? --Tom . The SQLCA defined for the ILE RPG for AS/400 has added the field SQLERRD which is defined as an array of six integers. the names for the SQLCA will remain as used with RPG for AS/400. Ted: I have an RPG IV program with an embedded SQL DELETE statement. The standard SQLCA names were changed to a length of 6 for RPG for AS/400. SQLERRD is defined to overlay the SQLERR definition. To maintain compatibility with RPG for AS/400 programs which are converted to ILE RPG for AS/400.

UPDATE. or INSERT operation completes normally. Data division. goback.DB2/400 returns this information to you through a subfield of the SQL communications area (SQLCA). Working-storage section. Main-logic. I understand that RPG is the only compiler that includes the SQLCA by default. PL/I. You can refer to this subfield by either of two names: SQLERRD(3) or SQLER3. Procedure division. Environment division. Exec SQL delete from qtemp/cuxt where company = 99 end-exec. In the following short COBOL program. SQL/400 From Wikibooks. SQL005. the SQLCA is included in the workingstorage section. consult the DB2 reference on IBM's Web site. C/exec sql C+ C+ C/end-exec /free delete from somefile where company = :company if SQLER3 > *zero. C. Program-ID. packed-decimal. Identification division. and FORTRAN programs must use the SQL INCLUDE command to reference the SQLDA. the COBOL. For more information about the SQLCA. Exec SQL set option commit=*none end-exec. if SQLERRD(3) > zero move "N" to EmptySet else move "Y" to EmptySet end-if. DB2 updates the subfield with the number of rows that qualified for the operation. a data area that is automatically included in your RPG program when you compile. Exec SQL include SQLCA end-exec. the open-content textbooks collection . According to the SQL Reference. 01 Company pic s999 01 EmptySet pic x. When a DELETE.

business eServer Computer Platform. IBM's Universal Data Base to get at the 400'ds files (tables) and their fields (columns). and can be rerun as needed. PL/1. and/or use the F4 Prompt function to navigate IBM DB2/400 data base. It comes in several flavors. SQL/400 is a proprietory dialect of SQL for the IBM AS/400. C/400. which was later rebranded as iSeries and finally as SystemI. unfamiliar with SQL. Interactive SQL/400 is often used to identify statements needed to perform various programming actions. Dynamic Embedded SQL All the AS/400 languages that support Static Embedded SQL also support Dynamic Embedded SQL. Invoke STRSQL {Start SQL/400} and get at what looks like another command line. where we can key in SQL statements for immediate execution. • Other SQL/400 Applications. SQL Query Manager is used to design and store SQL queries used to extract information. such as Microsoft Excel. with programs in different languages calling code in other languages a variety of ways. can be prompted through what is needed to create inquiries and reports that are more sophisticated than what can be created by other tools. Static Embedded SQL involves SQL statements hard coded inside another high level language program. and create procedures. we have the option of saving a log of our activity. You can put SQL statements inside an Excel cell which will access the AS/400 data base. search This is a work in progress to which a lot more info will be added. REXX. So the AS/400 programmer can pick and choose the Computer Language that is best suited to the needed function. Please be patient. Contents . from which our successful SQL/400 statements may be copied into one of the other flavors of SQL/400. for incorporation in the SQL statement. it immediately shows you the latest data from the data base. • • • • Interactive SQL/400 which can be run from the Command line.< SQL Jump to: navigation. Query Manager is an interface to SQL in which end users. such as RPG/400. When you open the Excel. You can then change the statements to change what data is selected for the Excel. Cobol/400. See Comparative Computer Languages. generate reports. the programming language. and Fortran/400. When exiting an Interactive SQL/400 session. which is a version of UDB. but there are some things they can do that SQL can not. or ways that SQL can be run. This is done because there are some areas where SQL is more powerful than other languages. Suppose you connect to the AS/400 using a Personal Computer which has many PC Applications.

prepared and run when the program is run. RPG identifies what section of a program.3.[hide] • • • • • • 1 Interactive SQL/400 2 SQL Query Manager 3 Languages that support Embedded SQL 4 Static Embedded SQL o 4. [edit] SQL embedded in RPG SQL statements can be placed within the calculations of an RPG program . using a character in position 6 with a C for calculations. Dynamic SQL Statements are composed. Here is how SQL statements are usually embedded in RPG program calculations.. detail calculations.3 Static SQL SELECT Modes  4.. total calculations. then an SQL Precompiler converts the SQL statements to a form that is acceptable to the high level language compiler.1 SQL Cursor 5 Dynamic Embedded SQL 6 Other SQL/400 Applications [edit] Interactive SQL/400 [edit] SQL Query Manager [edit] Languages that support Embedded SQL • • • • • • C/400 Cobol/400 RPG (many different versions of RPG on the 400) REXX PL/1 Fortran/400 [edit] Static Embedded SQL Static Embedded SQL statments are written into the source code of the high level language program. or an RPG Subroutine.2 SQL embedded in Cobol o 4. .1 SQL embedded in RPG o 4.

close the current use of it. the cursor is moved to the next row of the result table.C/EXEC SQL C+ SQL Statement C+ SQL Statement C+ SQL Statement C/END-EXEC There can be any number of SQL Statements. to get what the user wants to see. We cannot back up in a Serial Cursor. leading the Scroll Cursor to be positioned elsewhere in the data. at a time. When it is not specified that the Scroll Cursor is Dynamic. The END-EXEC must be terminated with a period. Notice the plus sign at the beginning of each SQL statement. SQL statements are embedded in specific program sections. Each time a row of data is read into the program. This is repeated until reaching end-of-data. while the Fetch statement can position the cursor where to do the reading. o A Serial Cursor is when each row of the results is to be fetched only once per open of the cursor. EXEC SQL SQL Statement SQL Statement END-EXEC. [edit] SQL embedded in Cobol In Cobol. To use the cursor again. o A Scroll Cursor supports alternatives to going through the data sequentially. who can then key in something to navigate the data. access is read-only. There can be any number of SQL statements. • • SELECT one row of a table. then re-issue the open statement. SELECT multiple rows of one or more tables into a Cursor. [edit] SQL Cursor There are several ways this can be programmed. when the cursor should be closed. Access defaults to the Serial Cursor method. o A Dynamic Scorll Cursor permits the update of rows that are accessed. going through the data sequentially. One way is to define a Data Structure which defines all the data elements that will be selected with each occurrence of . that delineate the beginning and end of the SQL statements embedded in the RPG code. [edit] Static SQL SELECT Modes Static SQL offers two modes of operation. with secondary options. Notice the slash at beginning of the first and last lines. This kind of Cursor is usually in a program to display parts of the data base on screen of end user.

> *ENDACTGRP *ENDMOD. where Variables can be defined wherever needed. it creates a cursor of the data you are selecting. SQL/400 requires that anything. I know there is thomthing like a Calable Statment. there is a parameter to close the cursor when the activation group or module is closed. Best Regards Rune Oynes Stuart Payne 06-10-1999. ( Close SQL cursor . embedded in another high level language. the sequence of processing an SQL cursor. It reads in the data from the joined files very similar to reading a record from one file in RPG or Cobol. 07:20 AM I have experience the same thing and from what I observe is the following. to be used in the program. Unlike many other AS/400 programming languages. The first time you call the statement. Thus. The default from IBM is *ENDACTGRP. which makes it run faster. . . be defined earlier in the program source code than where ever it is subsequently used.S. . 5. 08:35 PM I em using Prepared Statement. the next time i use the PS it is faster. When you create a SQLRPGLE. 2. with AS400 JDBC. 4. 3. is: 1. *ENDACTGRP ) . where the SQL is embedded. This cursor will be re-used for other statement calls. will this help and how do i create it on the AS400. .executing the multiple rows. . Why does this happend. This is similar to another index (logical file) on the table or an access path of an Open Query File. . and what can i do to make it faster also the first time i execute a P. . such at the beginning of some Subroutine that will be using them. (PS) The first time i uses the PS it is very slow. Declare Cursor Open Cursor Fetch Rows using Select Optionally Delete or Update Close Cursor [edit] Dynamic Embedded SQL AS/400 SQL performance Rune Oynes 06-06-1999.

when i se the message ODP reused. Another suggestion is to look at the "Optimize For n Rows" in the Select clause. Rune Oynes 06-10-1999. By using the "Optimize For ## Rows". the responce time is 3 sek. 11:29 PM IS IT POSIBLE TO PREVENT THE ODP to be creted eatch time a user start his application. the SQL optimzer will choose the most efficient way to return the number of rows found in ##. and then look at your job log. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- . To debug the SQL you execute the STRDBG command. you should probably e-mail him for any other techniques. To speed up the statement call the first time. 09:11 PM Rune Oynes 06-10-1999. look at the using another index (logical file). You can also try and open the data path by another application and leave it open. Since Mike C. I have tryed the debugger on AS400 (STRSRVJOB on the QZDASOINI job and STRDBG) I see the message ODP created. the optimize option. is the forum director. If you are re-ordering the data. File size is another factor. run your SQL. or put the SQL in debug and analyze what is happening. as well as selecting on certain values. When i se "ODP created" it takes 30sek before the resultset is ready. Best Ragards Rune Stuart Payne 06-16-1999. then use or create a logical file that has the same order. Caution is required on this action because it may effect other applications.1 or 2 times after that i get "ODP reused". the smaller the file. 05:10 AM You need the ODP. the quicker the ODP can be establish.So I am assuming that IBM keeps the SQL cursor open until the activation group has ended. There are some tricks you can try to speed up the ODP.

Convert Query/400 queries to Query Management Queries and Forms. sequence. Upon completion you'll be able to: • • • • • • • • • • Create a database (SQL collection) using SQL data definition language statements. audio-cassette course providing comprehensive. Use SQL calls in interactive HLL programs that dynamically create access paths to data. practical training in SQL/400 and its functions.Description iSeries SQL/400 is a self-paced. Use the SQL interactive environment to select. Pass parameters from HLL programs to Query Management Queries. and summarize data from SQL tables and iSeries files. Populate an SQL collection using SQL data manipulation language statements. SQL Data Manipulation Statements SELECT INSERT UPDATE WHERE GROUP BY UNION SQL Predicates and Functions Commitment Control Query Management Queries and Forms Creating Queries and Forms Passing HLL Parameters to Queries Converting Query/400 to QM Queries and Forms Interactive SQL/400 STRSQL (Start SQL) Command Setting and Saving SQL Session Defaults SQL Entry Screen Executing SQL Statements Embedded SQL/400 in HighLevel Languages COBOL and RPG Examples Accessing SQL Tables and Cursors Serial and Scrollable Cursors Updateable Cursors Dynamic Data Selection SQL Precompiler SQL Optimizer SQL/400 Subqueries Interactive and Embedded Simple Subqueries Compound Subqueries Correlated Subqueries SQL Concepts Using SQL across Multiple Operating Systems Collections Tables Rows and Columns Views Indexes Cursors Communication Areas SQL Data Definition Statements CREATE COLLECTION CREATE TABLE CREATE VIEW CREATE INDEX Documenting the Data Base Model . Create reports using Query Management Queries and Forms. Use SQL statements to access physical and logical files created outside of the SQL collection. Use SQL queries to extract information about the SQL collection itself. Use SQL interfaces within RPG/400 and COBOL/400 programs. join.

outer joins. this hands-on SQL training shows you plenty of short-cuts. summing and counting See how using GROUP BY gives you "level break" type functionality in SQL Develop and test RPG code with joins: inner join. you’ll embed SQL statements in your RPG code. NOT and multiple conditions • • • • Work with column functions for statistics. IN. This is gold! . gives you tips and real-world advice on how to develop and test statements. serial. UPDATE and DELETE statements to make changes. without needing to call system commands. scroll and dynamic: know when and how to use each Boost SQL performance with DECLARE and PREPARE SELECT statement options to get just the data you want: LIKE. using subselects. You'll work with data returned by SELECT statements and use INSERT. you’ll learn by running actual code on your system. SQL is a powerful. With SQL you’ll put more "ease of use" and functionality into your RPG programs. copy/paste the working statements into RPG Work with data returned from SQL SELECT statements in your RPG programs using cursors Cursors: read-only and updateable. nested tables and joins Simplify SQL error handling in your code with the WHENEVER statement Use the SQL Communication Area to get all information about an SQL error or warning • • • • • • You’ll learn how to create new files quickly and easily in an RPG program. test and debug RPG code using embedded SQL. you’ll add the power and convenience of SQL to your RPG programming. BETWEEN. if you program with RPG. SQL for RPG Programmers includes over a dozen RPG coding labs where you'll develop. correlated subqueries. Here's is what you will learn: • • Work with host variables to pass data from RPG to SQL statements Unsure of the syntax of an SQL statement? Develop and test SQL statements in the Interactive SQL environment. there's no better way to learn SQL than to actually see how it is used in RPG programs. exception join and cross join The power of one: how to combine SQL statements into one statement. convenient and universal tool and.With SQL for RPG Programmers you’ll quickly modernize your RPG skills. Using the SQL pre-compiler. Most importantly. Developed by programmers for programmers. Instead of relying solely on limited RPG file operations.

Sign up to vote on this title
UsefulNot useful