Natural for DB2 Beyond The Find and Read

  • 431 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
431
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
10
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • These tables were created from the demo Employees and Vehicles file
  • Like the NATURAL FIND, we can also code READ
  • On the LISTSQL, notice that the thru gets translated to a >=. DB2 does not do a true browse. The resulting table will be much larger than the query we might suppose.
  • Notice that we do not receive a code of 100 on this query, Natural limits the read rather than DB2
  • Notice the much higher timreons for this query, and the fact that it turns around and does a table space access rather than index.
  • The safest way to avoid this is to never code a READ. The FIND allows much more flexibility, just add the sorted by to provide the order that would be implied by the read.
  • Natural V4 does allow an exception to this rule. Replace the ‘THRU’ with a ‘TO’
  • Notice that this gets replace with a ‘BETWEEN’ select.
  • Notice the much lower timerons, and the index path.
  • DB2 provides SPUFI or QMF and NATURAL provides for ISQL. These are pure SQL tools. Use of this took in NATURAL does not require that a DDM (VIEW) be created.
  • These tools automatically create headers and the display.
  • The * notations is not the same as it is with pure SQL coding. The * inicates that it should look at the view notation to build the list of columns for the SELECT. The into view notation also gives the list of host variables for the select output.
  • In place of the * notation, we may list the fields we want to use. This allows more specific selection.
  • The select allows more options than the FIND. Here we can use a ‘LIKE’ condition which allows for wild-cards. With the FIND, we would have to build two variables and build a more complicated condition.
  • The in lets us build a list of values to match on.
  • This is similar to the NATURAL ‘THRU’ but it is evaluated by DB2 not NATURAL
  • To connect two files, we can build a nested select. Where the conditions of the second select are based on a value returned from the first select. In this case the second select will be executed once for each row returned from the first select.
  • We are going to log this.
  • Here we allow a filter on the logging. We are not going to log the fetch cursors (this will prevent an overflow of the logging area)
  • We run the query and then instruct it to show the log.
  • Notice the main select is executed once and the second on 9 times.
  • The LISTSQL will show two selcects .
  • The cost of the first SELECT is 5.3.
  • The second SELECT
  • The cost of the second SELECT is 3.7
  • To calcuate the cost we must calculate.
  • We can do all of this with a single select with what is know as a join. We create aliases or corralation names to make reference easier.
  • Notice that the cost of this is 26.5 vs 38.6 of the original nested query.
  • Notice that the cost of this is 26.5 vs 38.6 of the original nested query.
  • With the nested select, we must add a ‘IF NO RECORDS found clause to insure we can get the details from the first SELECT.
  • Notice that we now have a record for Marion Moeller that we did not see with the first query.
  • To insure all of the rows from the first select, are show, we must code what is a left outer join.
  • We can build more complicated queries by making the one of the joining conditions be built by a select on the table itself..
  • We can create new fields on the fly with the SELECT. Here we calculate a total pay field by adding the salary and bonus fields.
  • This a local data area that is use to report totals for various leave columns
  • In addition to bringing back columns of data, we can tell DB2 to bring back the sum of the values in the column. This shows summing one column, but we could sum many columns in one call as long as the where clause is right for all of the sums.

Transcript

  • 1. “”” Natural for DB2 Beyond The Find and Read Darrell Skildum Advisory Systems Engineer Software Ag
  • 2. DB2 Programming Tools
    • DBLOG
      • Traces all SQL calls
      • Entry point to LISTSQL and EXPLAIN
    • LISTSQL
      • Lists SQL generated
      • Entry point to EXPLAIN
    • EXPLAIN
      • DB2 estimate of cost of command
      • Shows INDEX use/non use
      • Shows columns of index used
  • 3. ADABAS vs DB2 Purpose is not to debate merits of each Database, but to encourage the best use of the tools for each
  • 4. ADABAS vs DB2
    • ADABAS is a Record Oriented Database
      • A FIND results in a set of ISNs which can be retrieved one at a time
      • A READ is a true browse function
    • DB2 is set oriented
      • A FIND translates to a SELECT which produces a subset of the records as a separate table which is then browsed.
      • A READ may result is SELECT GE which may produce a subset much larger than wanted
  • 5. Adabas vs DB2 programming
    • For the most part, the same syntax works for both ADABAS and DB2
    • More thought should be applied because in some cases, the same syntax may be less efficient in DB2
    • DB2 allows (requires) much more complicated syntax to effectively retrieve the selected information
  • 6. Creation of Tables
    • Employees Creator = SADRS
    Employees ADA EMPL_BASE EMPLOYEE_PAY EMPLOYEE_LEAVE EMPLOY_DE EMPLOY_UK EMPLOY_SP EMPLOY_US EMPLOY_FR
  • 7. Creation of Tables
    • Vehicles -Creator = SADRS
    Vehicles (ADA) VEHICLES
  • 8. Natural Read
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF SADRS-EMPL_BASE
    • 2 PERSONNEL_ID
    • 2 NAME
    • 2 FIRST_NAME
    • 2 DEPT
    • END-DEFINE
    • READ EMP WITH DEPT = 'COMP21' THRU 'COMP21'
    • DISPLAY PERSONNEL_ID NAME FIRST_NAME
    • END-READ
    • END
  • 9. Output from Read
    • Page 1
    • PERSONNEL_ID NAME FIRST_NAME
    • ------------ -------------------- --------------------
    • 11300324 DAMMBACH FLORIAN
    • 11400325 SPAEHT KLAUS
    • 11700324 MOELLER MARION
    • 11700323 LIST FRANZ
    • 11300323 DAMM WOLFGANG
    • 11300320 BRUNNER HELMUT
    • 11100114 UNGER KLAUS
    • 11100113 TREIBER KLAUS
    • 11100105 SCHIRM CHRISTIAN
  • 10. List SQL from READ
    • Member NATREAD - LISTSQL - Library NDBDEMO
    • NATURAL statement at line 0080 Stmt 1 / 1
    • READ EMP WITH DEPT = 'COMP21' THRU 'COMP21'
    • Generated SQL statement Mode : dynamic DBRM : Line 1 / 5
    • SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT
    • FROM SADRS.EMPL_BASE
    • WHERE DEPT >= 'COMP21'
    • ORDER BY DEPT
    • FOR FETCH ONLY
  • 11. DBLOG from READ
    • User SADRS - DBLOG Trace - Library NDBDEMO
    • M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV
    • _ 1 ROLLBACK 00 00 0000 D DB2 6660 00
    • _ 2 SELECT PERSONNEL_ID,NAME,FIRS 01 01 0080 D DB2 NATREAD 0080 01
    • _ 3 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 4 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 5 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 6 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 7 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 8 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 9 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 10 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 11 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 12 FETCH CURSOR NEX 01 01 0080 D DB2 NATREAD 0080 01
    • _ 13 CLOSE CURSOR 01 01 0080 D DB2 NATREAD 0080 01
  • 12. Explain from Read
    • Estimated cost : 428.5 timerons
    • Qblockno Mixop Access Match Index Pre- Access- Column-
    • Planno seq type cols only fetch creator name fn_eval
    • --- --- --- ---- ----- ----- ----- -------- ------------------ -
    • 1 1 R S
    • 1 2
    • Table Tslock -- sortn -- -- sortc --
    • Tabno Creator Tname mode Method uq jo or gr uq jo or gr
    • --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- --
    • 1 SADRS EMPL_BASE IS N N N N N N N N
    • 3 N N N N N N Y N
  • 13. Read
    • READ
  • 14. Read Natural V4
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF SADRS-EMPL_BASE
    • 2 PERSONNEL_ID
    • 2 NAME
    • 2 FIRST_NAME
    • 2 DEPT
    • END-DEFINE
    • READ EMP WITH DEPT = 'COMP21' TO 'COMP21'
    • DISPLAY PERSONNEL_ID NAME FIRST_NAME
    • END-READ
    • END
  • 15. ListSQL V4 Read
    • Member NATREAD2 - LISTSQL - Library NDBDEMO
    • NATURAL statement at line 0080 Stmt 1 / 1
    • READ EMP WITH DEPT = 'COMP21' TO 'COMP21'
    • Generated SQL statement Mode : dynamic DBRM : Line 1 / 5
    • SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT
    • FROM SADRS.EMPL_BASE
    • WHERE DEPT BETWEEN 'COMP21' AND 'COMP21'
    • ORDER BY DEPT
    • FOR FETCH ONLY
  • 16. Explain for V4 Read
    • Estimated cost : 16.5 timerons
    • Qblockno Mixop Access Match Index Pre- Access- Column-
    • Planno seq type cols only fetch creator name fn_eval
    • --- --- --- ---- ----- ----- ----- -------- ------------------ -
    • 1 1 I 1 L SADRS DEPT_NAME
    • 1 2
    • Table Tslock -- sortn -- -- sortc --
    • Tabno Creator Tname mode Method uq jo or gr uq jo or gr
    • --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- --
    • 1 SADRS EMPL_BASE IS N N N N N N N N
    • 3 N N N N N N Y N
    16.5 timerons vs 428.5
  • 17. Coding SQL SPUFI - ISQL ***** ********************** 00001 SELECT * 00002 FROM SADRS.EMPL_BASE 00003 WHERE DEPT = 'COMP02' ***** **********************
  • 18. Coding SQL SPUFI - ISQL ----------------------------------------------------------------- PERSONNEL_ID FIRST_NAME MIDDLE_NAME NAME >---------------------------------------------------------------- 50003700 LOUIS D'AGOSTINO 50016600 ANNIE GODEFROY 50019000 ROGER BESSON 50020600 DANIEL GREGOIRE 50006700 SERGE RIGOLLET 60000231 ANTONIO VILLAR 60000509 VICTORIA RODRIGUEZ 60008045 GORKA NIEDA 60008231 JORDI MONTERREY ESPLA 60008745 ANTONIO PUERTAS 60008509 MARIA JOSE PUERTOLAS
  • 19. Coding SQL DEFINE DATA LOCAL 1 EMP VIEW OF SADRS-EMPL_BASE 2 PERSONNEL_ID 2 NAME 2 FIRST_NAME 2 DEPT END-DEFINE SELECT * INTO VIEW EMP FROM SADRS-EMPL_BASE WHERE DEPT = 'COMP02' DISPLAY PERSONNEL_ID NAME FIRST_NAME END-SELECT END
  • 20. Coding SQL DEFINE DATA LOCAL 1 EMP VIEW OF SADRS-EMPL_BASE 2 PERSONNEL_ID 2 NAME 2 FIRST_NAME 2 DEPT END-DEFINE INTO VIEW EMP FROM SADRS-EMPL_BASE WHERE DEPT = 'COMP02' DISPLAY PERSONNEL_ID NAME FIRST_NAME END-SELECT END SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT
  • 21. LIKE
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF SADRS-EMPL_BASE
    • 2 PERSONNEL_ID
    • 2 NAME
    • 2 FIRST_NAME
    • 2 DEPT
    • END-DEFINE
    • SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT
    • INTO EMP.PERSONNEL_ID, EMP.NAME, EMP.FIRST_NAME, EMP.DEPT
    • FROM SADRS-EMPL_BASE
    • WHERE DEPT LIKE 'COMP0%'
    • DISPLAY EMP.PERSONNEL_ID EMP.NAME EMP.FIRST_NAME EMP.DEPT
    • END-SELECT
    • END
  • 22. LIKE RESULTS
    • PERSONNEL_ID NAME FIRST_NAME DEPT
    • ------------ -------------------- -------------------- ------
    • 50003700 D'AGOSTINO LOUIS COMP02
    • 50003100 HEURTEBISE MICHEL COMP01
    • 50001700 TANCHOU GERARD COMP01
    • 50001200 SIECA FRANCOIS COMP01
    • 50000300 FISCHER RENE COMP03
    • 50014000 LEFEBVRE CLAUDE COMP01
    • 50015800 CATILLON MARCEL COMP03
    • 50016000 ROLLET GEORGES COMP01
    • 50016600 GODEFROY ANNIE COMP02
    • 50019000 BESSON ROGER COMP02
    • 50019500 YOT RENE COMP01
    • 50020600 GREGOIRE DANIEL COMP02
    • 50000100 GASET JEAN COMP01
    • 50000500 RIVIERE JEAN-LUC COMP05
    • 50006800 THIEBAULT ROBERT COMP03
    • 50006700 RIGOLLET SERGE COMP02
    • 50006600 TEYSSIER MICHEL COMP01
    • 60000231 VILLAR ANTONIO COMP02
  • 23. IN
    • SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT
    • INTO EMP.PERSONNEL_ID, EMP.NAME, EMP.FIRST_NAME, EMP.DEPT
    • FROM SADRS-EMPL_BASE
    • WHERE DEPT IN ('COMP02', 'COMP03', 'COMP21')
    • DISPLAY EMP.PERSONNEL_ID EMP.NAME EMP.FIRST_NAME EMP.DEPT
    • END-SELECT
  • 24. IN - Results
    • PERSONNEL_ID NAME FIRST_NAME DEPT
    • ------------ -------------------- -------------------- ------
    • 50003700 D'AGOSTINO LOUIS COMP02
    • 50000300 FISCHER RENE COMP03
    • 50015800 CATILLON MARCEL COMP03
    • 50016600 GODEFROY ANNIE COMP02
    • 50019000 BESSON ROGER COMP02
    • 50020600 GREGOIRE DANIEL COMP02
    • 50006800 THIEBAULT ROBERT COMP03
    • 50006700 RIGOLLET SERGE COMP02
    • 11100105 SCHIRM CHRISTIAN COMP21
    • 11100113 TREIBER KLAUS COMP21
    • 11100114 UNGER KLAUS COMP21
    • 11300320 BRUNNER HELMUT COMP21
    • 11300323 DAMM WOLFGANG COMP21
    • 11700323 LIST FRANZ COMP21
    • 11700324 MOELLER MARION COMP21
  • 25. BETWEEN
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF SADRS-EMPL_BASE
    • 2 PERSONNEL_ID
    • 2 NAME
    • 2 FIRST_NAME
    • 2 DEPT
    • END-DEFINE
    • SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT
    • INTO EMP.PERSONNEL_ID, EMP.NAME, EMP.FIRST_NAME, EMP.DEPT
    • FROM SADRS-EMPL_BASE
    • WHERE DEPT BETWEEN 'COMP02' AND 'COMP05'
    • DISPLAY EMP.PERSONNEL_ID EMP.NAME EMP.FIRST_NAME EMP.DEPT
    • END-SELECT
  • 26. BETWEEN - RESULTS
    • PERSONNEL_ID NAME FIRST_NAME DEPT
    • ------------ -------------------- -------------------- ------
    • 50003700 D'AGOSTINO LOUIS COMP02
    • 50000300 FISCHER RENE COMP03
    • 50015800 CATILLON MARCEL COMP03
    • 50016600 GODEFROY ANNIE COMP02
    • 50019000 BESSON ROGER COMP02
    • 50020600 GREGOIRE DANIEL COMP02
    • 50000500 RIVIERE JEAN-LUC COMP05
    • 50006800 THIEBAULT ROBERT COMP03
    • 50006700 RIGOLLET SERGE COMP02
    • 60000231 VILLAR ANTONIO COMP02
    • 60000509 RODRIGUEZ VICTORIA COMP02
    • 60008045 NIEDA GORKA COMP02
    • 60008100 PELEGRIN MANUEL COMP03
    • 60008231 ESPLA JORDI COMP02
    • 60008745 PUERTAS ANTONIO COMP02
    • 60008168 MARTINEZ ANTONIO COMP03
    • 60008287 PEREZ ANTONIO COMP03
    • 60008509 PUERTOLAS MARIA JOSE COMP02
  • 27. Nested Select DEFINE DATA LOCAL 1 EMP VIEW OF SADRS-EMPL_BASE 2 PERSONNEL_ID 2 NAME 2 FIRST_NAME 2 DEPT 1 VEH VIEW OF SADRS-VEHICLES 2 PERSONNEL_ID 2 MAKE 2 MODEL 2 REG_NUM END-DEFINE SELECT * INTO VIEW EMP FROM SADRS-EMPL_BASE WHERE DEPT = 'COMP21' SELECT * INTO VIEW VEH FROM SADRS-VEHICLES WHERE PERSONNEL_ID = EMP.PERSONNEL_ID DISPLAY PERSONNEL_ID FIRST_NAME NAME MAKE END-SELECT
  • 28. Nested Select test dblog q ? > + Program SQLEX5 Lib NDBDEMO p ....+....1....+....2....+....3....+....4....+....5....+....6....+....7 0010 DEFINE DATA LOCAL 0020 1 EMP VIEW OF SADRS-EMPL_BASE 0030 2 PERSONNEL_ID 0040 2 NAME 0050 2 FIRST_NAME 0060 2 DEPT 0070 1 VEH VIEW OF SADRS-VEHICLES 0080 2 PERSONNEL_ID 0090 2 MAKE 0100 2 MODEL 0110 2 REG_NUM 0120 END-DEFINE 0130 SELECT * INTO VIEW EMP 0140 FROM SADRS-EMPL_BASE 0150 WHERE DEPT = 'COMP21' 0160 SELECT * INTO VIEW VEH 0170 FROM SADRS-VEHICLES 0180 WHERE PERSONNEL_ID = EMP.PERSONNEL_ID 0190 IF NO RECORDS FOUND 0200 MOVE 'NONE' TO VEH.MAKE
  • 29. Nested Select User SADRS - DBLOG Menu - Library NDBDEMO Code Function ---- ------------------------------------ B Begin Logging of SQL Statements E End and Display Log Records S Snapshot of Specific SQL Statement . Exit ---- ------------------------------------ Code .. B Statement .. select__ Skip ....... _____ Program .... ________ Line from .. 0000 Low SQLC .. ______ High SQLC .. ______ Line to .... 0000
  • 30. Nested Select NEXT test dblog q show LIB=NDBDEMO Page 1 05-04-06 01:47:57 PERSONNEL_ID FIRST_NAME NAME MAKE ------------ -------------------- -------------------- -------------------- 11100105 CHRISTIAN SCHIRM OPEL 11100113 KLAUS TREIBER AUDI 11100114 KLAUS UNGER BMW 11300320 HELMUT BRUNNER AUDI 11300323 WOLFGANG DAMM VW 11700323 FRANZ LIST VW 11700324 MARION MOELLER NONE 11400325 KLAUS SPAEHT AUDI 11300324 FLORIAN DAMMBACH VW
  • 31. Nested Select User SADRS - DBLOG Trace - Library NDBDEMO M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV _ 1 SELECT PERSONNEL_ID,NAME,FIRS 03 03 0130 D DB2 SQLEX5 0130 01 _ 2 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 3 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 4 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 5 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 6 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 7 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 8 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 9 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 10 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01
  • 32. Nested Select Member SQLEX5 - LISTSQL - Library NDBDEMO NATURAL statement at line 0130 Stmt 1 / 2 SELECT * INTO VIEW EMP FROM SADRS-EMPL_BASE WHERE DEPT = 'COMP21' Generated SQL statement Mode : dynamic DBRM : Line 1 / 4 SELECT PERSONNEL_ID, NAME, FIRST_NAME, DEPT FROM SADRS.EMPL_BASE WHERE DEPT = 'COMP21' FOR FETCH ONLY
  • 33. Nested Select Queryno 1 EXPLAIN Result Row 1 / 1 Estimated cost : 5.3 timerons Qblockno Mixop Access Match Index Pre- Access- Column- Planno seq type cols only fetch creator name fn_eval --- --- --- ---- ----- ----- ----- -------- ------------------ - 1 1 I 1 L SADRS DEPT_NAME Table Tslock -- sortn -- -- sortc -- Tabno Creator Tname mode Method uq jo or gr uq jo or gr --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- -- 1 SADRS EMPL_BASE IS N N N N N N N N
  • 34. Nested Select Member SQLEX5 - LISTSQL - Library NDBDEMO NATURAL statement at line 0160 Stmt 2 / 2 SELECT * INTO VIEW VEH FROM SADRS-VEHICLES WHERE PERSONNEL_ID = EMP.PERSONNEL_ID Generated SQL statement Mode : dynamic DBRM : Line 1 / 4 SELECT PERSONNEL_ID, MAKE, MODEL, REG_NUM FROM SADRS.VEHICLES WHERE PERSONNEL_ID =? FOR FETCH ONLY
  • 35. Nested Select Queryno 1 EXPLAIN Result Row 1 / 1 Estimated cost : 3.7 timerons Qblockno Mixop Access Match Index Pre- Access- Column- Planno seq type cols only fetch creator name fn_eval --- --- --- ---- ----- ----- ----- -------- ------------------ - 1 1 I 1 VEHICLES PERSSNNEL_ID Table Tslock -- sortn -- -- sortc -- Tabno Creator Tname mode Method uq jo or gr uq jo or gr --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- -- 1 SADRS VEHICLES IS N N N N N N N N
  • 36. Nested Select PERSONNEL_ID FIRST_NAME NAME MAKE ------------ -------------------- -------------------- -------------------- 11100105 CHRISTIAN SCHIRM OPEL 11100113 KLAUS TREIBER AUDI 11100114 KLAUS UNGER BMW 11300320 HELMUT BRUNNER AUDI 11300323 WOLFGANG DAMM VW 11700323 FRANZ LIST VW 11400325 KLAUS SPAEHT AUDI 11300324 FLORIAN DAMMBACH VW
  • 37. Nested Select User SADRS - DBLOG Trace - Library NDBDEMO M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV _ 1 SELECT PERSONNEL_ID,NAME,FIRS 03 03 0130 D DB2 SQLEX5 0130 01 _ 2 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 3 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 4 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 5 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 6 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 7 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 8 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 9 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 _ 10 SELECT PERSONNEL_ID,MAKE,MODE 04 04 0160 D DB2 SQLEX5 0160 01 Total cost = 1*5.3 + 9*3.7 = 38.6
  • 38. Normal Join DEFINE DATA LOCAL 1 EMP VIEW OF SADRS-EMPL_BASE 2 NAME 2 PERSONNEL_ID 2 DEPT 2 FIRST_NAME 1 VEH VIEW OF SADRS-VEHICLES 2 MAKE 2 MODEL 2 PERSONNEL_ID 2 REG_NUM END-DEFINE SELECT A.NAME, A.PERSONNEL_ID, A.DEPT, A.FIRST_NAME, B.MAKE, B.MODEL, B.PERSONNEL_ID, B.REG_NUM INTO VIEW EMP,VEH FROM SADRS-EMPL_BASE A , SADRS-VEHICLES B WHERE DEPT = 'COMP21' AND B.PERSONNEL_ID = A.PERSONNEL_ID ORDER BY A.NAME DISPLAY EMP.NAME EMP.PERSONNEL_ID VEH.MAKE DEPT Correlation Name Alias or
  • 39. Join Results PERSONNEL_ID FIRST_NAME NAME MAKE ------------ -------------------- -------------------- -------------------- 11100105 CHRISTIAN SCHIRM OPEL 11100113 KLAUS TREIBER AUDI 11100114 KLAUS UNGER BMW 11300320 HELMUT BRUNNER AUDI 11300323 WOLFGANG DAMM VW 11700323 FRANZ LIST VW 11400325 KLAUS SPAEHT AUDI 11300324 FLORIAN DAMMBACH VW
  • 40. Join ListSql Member SQLEX6 - LISTSQL - Library NDBDEMO NATURAL statement at line 0130 Stmt 1 / 1 SELECT A.NAME, A.PERSONNEL_ID, A.DEPT, A.FIRST_NAME, B.MAKE, B.MODEL, B.PERSONNEL_ID, B.REG_NUM INTO VIEW EMP,VEH Generated SQL statement Mode : dynamic DBRM : Line 1 / 6 SELECT A.NAME, A.PERSONNEL_ID, A.DEPT, A.FIRST_NAME, B.MAKE, B.MODEL, B .PERSONNEL_ID, B.REG_NUM FROM SADRS.EMPL_BASE A, SADRS.VEHICLES B WHERE DEPT = 'COMP21' AND B.PERSONNEL_ID = A.PERSONNEL_ID ORDER BY A.NAME FOR FETCH ONLY
  • 41. Join Explain Estimated cost : 26.5 timerons Qblockno Mixop Access Match Index Pre- Access- Column- Planno seq type cols only fetch creator name fn_eval --- --- --- ---- ----- ----- ----- -------- ------------------ - 1 1 I 1 L SADRS DEPT_NAME 1 2 I 1 VEHICLES PERSSNNEL_ID 1 3 Table Tslock -- sortn -- -- sortc -- Tabno Creator Tname mode Method uq jo or gr uq jo or gr --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- -- 1 SADRS EMPL_BASE IS N N N N N N N N 2 SADRS VEHICLES IS 1 N N N N N N N N 3 N N N N N N Y N vs 38.6
  • 42. Join Explain Estimated cost : 26.5 timerons Qblockno Mixop Access Match Index Pre- Access- Column- Planno seq type cols only fetch creator name fn_eval --- --- --- ---- ----- ----- ----- -------- ------------------ - 1 1 I 1 L SADRS DEPT_NAME 1 2 I 1 VEHICLES PERSSNNEL_ID 1 3 Table Tslock -- sortn -- -- sortc -- Tabno Creator Tname mode Method uq jo or gr uq jo or gr --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- -- 1 SADRS EMPL_BASE IS N N N N N N N N 2 SADRS VEHICLES IS 1 N N N N N N N N 3 N N N N N N Y N vs 38.6
  • 43. Nested Select – No Records SELECT * INTO VIEW EMP FROM SADRS-EMPL_BASE WHERE DEPT = 'COMP21' SELECT * INTO VIEW VEH FROM SADRS-VEHICLES WHERE PERSONNEL_ID = EMP.PERSONNEL_ID IF NO RECORDS FOUND MOVE 'NONE' TO VEH.MAKE END-NOREC DISPLAY EMP.PERSONNEL_ID FIRST_NAME NAME MAKE END-SELECT END-SELECT
  • 44. Nested Select – No Records PERSONNEL_ID FIRST_NAME NAME MAKE ------------ -------------------- -------------------- -------------------- 11100105 CHRISTIAN SCHIRM OPEL 11100113 KLAUS TREIBER AUDI 11100114 KLAUS UNGER BMW 11300320 HELMUT BRUNNER AUDI 11300323 WOLFGANG DAMM VW 11700323 FRANZ LIST VW 11700324 MARION MOELLER NONE 11400325 KLAUS SPAEHT AUDI 11300324 FLORIAN DAMMBACH VW
  • 45. Left Outer Join SELECT A.NAME, A.PERSONNEL_ID, A.DEPT, B.MAKE, B.MODEL, B.PERSONNEL_ID INTO VIEW EMP,VEH FROM (SELECT NAME,PERSONNEL_ID, DEPT FROM SADRS-EMPL_BASE WHERE DEPT = 'COMP21') AS A LEFT OUTER JOIN (SELECT MAKE, MODEL,PERSONNEL_ID FROM SADRS-VEHICLES) AS B ON B.PERSONNEL_ID = A.PERSONNEL_ID ORDER BY A.NAME IF VEH.MAKE = ' ' MOVE '***NONE***' TO VEH.MAKE END-IF DISPLAY EMP.NAME EMP.PERSONNEL_ID VEH.MAKE DEPT END-SELECT
  • 46. Left Outer Join Results NAME PERSONNEL_ID MAKE DEPT -------------------- ------------ -------------------- ------ BRUNNER 11300320 AUDI COMP21 DAMM 11300323 VW COMP21 DAMMBACH 11300324 VW COMP21 LIST 11700323 VW COMP21 MOELLER 11700324 ***NONE*** COMP21 SCHIRM 11100105 OPEL COMP21 SPAEHT 11400325 AUDI COMP21 TREIBER 11100113 AUDI COMP21 UNGER 11100114 BMW COMP21
  • 47. SubQuery
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF SADRS-EMPL_BASE
    • 2 PERSONNEL_ID
    • 2 NAME
    • 2 FIRST_NAME
    • 1 SAL VIEW OF SADRS-EMPLOYEE_PAY
    • 2 PAY_SEQ
    • 2 SALARY
    • END-DEFINE
    • SELECT A.PERSONNEL_ID, A.NAME, A.FIRST_NAME, B.PAY_SEQ,
    • B.SALARY INTO EMP.PERSONNEL_ID, EMP.NAME, EMP.FIRST_NAME,
    • SAL.PAY_SEQ, SAL.SALARY
    • FROM SADRS-EMPL_BASE A, SADRS-EMPLOYEE_PAY B
    • WHERE A.PERSONNEL_ID = '11100114'
    • AND B.PERSONNEL_ID = A.PERSONNEL_ID
    • AND PAY_SEQ = (SELECT MAX(PAY_SEQ) FROM SADRS-EMPLOYEE_PAY
    • WHERE PERSONNEL_ID = A.PERSONNEL_ID)
    • DISPLAY NAME SALARY PAY_SEQ
    • END-SELECT
  • 48. SubQuery Results
    • Page 1
    • NAME SALARY PAY_SEQ
    • -------------------- ---------- -----------
    • UNGER 46000 4
  • 49. SubQuery and calculated field
    • SELECT A.PERSONNEL_ID, A.NAME, A.FIRST_NAME, B.PAY_SEQ,
    • B.SALARY + B.BONUS1 + B.BONUS2 + B.BONUS3 AS TOTAL_PAY
    • INTO EMP.PERSONNEL_ID, EMP.NAME, EMP.FIRST_NAME,
    • SAL.PAY_SEQ, #TOTAL-PAY
    • FROM SADRS-EMPL_BASE A, SADRS-EMPLOYEE_PAY B
    • WHERE A.PERSONNEL_ID = '11100112'
    • AND B.PERSONNEL_ID = A.PERSONNEL_ID
    • AND PAY_SEQ = (SELECT MAX(PAY_SEQ) FROM SADRS-EMPLOYEE_PAY
    • WHERE PERSONNEL_ID = A.PERSONNEL_ID)
    • DISPLAY NAME #TOTAL-PAY PAY_SEQ
    • END-SELECT
  • 50. SubQuery and calculated field
    • Page 1
    • NAME #TOTAL-PAY PAY_SEQ
    • -------------------- ----------- -----------
    • THOMA 46000.00 4
  • 51. USING DB2 functions DEFINE DATA PARAMETER 01 #SSN (A9) 01 #TODAY (A10) 01 #BEGIN-DATEA (A10) 01 #BEGIN-ANNUAL (P4.2) 01 #EARNED-ANNUAL (P4.2) 01 #TAKEN-ANNUAL (P4.2) 01 #ADJUST-ANNUAL (P4.2) 01 #CURR-ANNUAL (P4.2) 01 #PENDING-ANNUAL (P4.2) 01 #AVAIL-ANNUAL (P4.2) 01 #BEGIN - DATES (A10)
  • 52. USING DB2 functions SELECT SUM(ALVB50D_NOHOUR) INTO #EARNED-ANNUAL FROM PALV-ALVB50D WHERE ALVB50D_NOSSN = #SSN AND ALVB50D_CDTYLV = 'AN' AND ALVB50D_CDTYTR = 'E' AND ALVB50D_DCLVE >= #BEGIN-DATEA GROUP BY ALVB50D_NOSSN END-SELECT
  • 53. Union
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF SADRS-EMPL_BASE
    • 2 PERSONNEL_ID
    • 2 NAME
    • 2 FIRST_NAME
    • 2 DEPT
    • 2 COUNTRY
    • END-DEFINE
    • SELECT *
    • INTO VIEW EMP
    • FROM SADRS-EMPLOY_DE
    • WHERE DEPT LIKE 'COMP%%'
    • UNION (SELECT * FROM SADRS-EMPLOY_UK
    • WHERE DEPT LIKE 'COMP%%')
    • ORDER BY NAME
    • DISPLAY PERSONNEL_ID FIRST_NAME NAME DEPT COUNTRY
    • END-SELECT
    • END
  • 54. Union Results
    • PERSONNEL_ID FIRST_NAME NAME DEPT COUNTRY
    • ------------ -------------------- -------------------- ------ -------
    • 30000231 ROBERT ACHIESON COMP02 UK
    • 30021544 ELIZABETH AKROYD COMP03 UK
    • 30034231 ROBIN ARCHER COMP02 UK
    • 11500327 HANS BACHMANN COMP35 D
    • 30008042 PAULINE BAKER COMP12 UK
    • 30000287 JACK BIRKENSHAW COMP12 UK
    • 30034311 IAN BRANGWIN COMP01 UK
    • 30008509 CAROLINE BROOKS COMP02 UK
    • 11300320 HELMUT BRUNNER COMP21 D
    • 11300321 MANFRED BUCHERT COMP25 D
    • 30021630 ANDREW BULLOCK COMP03 UK
    • 11100110 GEORG BUNGERT COMP25 D
    • 11300322 FRANZ BURKNER COMP25 D
    • 30034107 COLIN CARROLL COMP01 UK
    • 30016045 JULIAN CHESTER COMP02 UK
    • 30034509 IAN COLQHOUN COMP12 UK
    • 11300323 WOLFGANG DAMM COMP21 D
    • 11300324 FLORIAN DAMMBACH COMP21 D
  • 55. Updating
    • 1 EMP VIEW OF I038-EMPLOYEES
    • 2 NAME
    • 2 PERSONNEL_ID
    • 2 SALARY_1
    • 1 EMP2 VIEW OF I038-EMPLOYEES
    • 2 NAME
    • 2 PERSONNEL_ID
    • 2 SALARY_1
    • 1 #PID (A8)
    • END-DEFINE
    • INPUT #PID
    • SELECT * INTO VIEW EMP FROM I038-EMPLOYEES
    • WHERE PERSONNEL_ID = #PID
    • END-SELECT
    • INPUT (AD=M) EMP.PERSONNEL_ID EMP.NAME EMP.SALARY_1
    • SELECT * INTO VIEW EMP2 FROM I038-EMPLOYEES
    • WHERE PERSONNEL_ID = #PID
    • MOVE BY NAME EMP TO EMP2
    • UPDATE
    • END-SELECT
  • 56. Updating
    • 1 EMP VIEW OF I038-EMPLOYEES
    • 2 NAME
    • 2 PERSONNEL_ID
    • 2 SALARY_1
    • 1 EMP2 VIEW OF I038-EMPLOYEES
    • 2 NAME
    • 2 PERSONNEL_ID
    • 2 SALARY_1
    • 1 #PID (A8)
    • END-DEFINE
    • REPEAT
    • INPUT #PID
    • SELECT * INTO VIEW EMP FROM I038-EMPLOYEES
    • WHERE PERSONNEL_ID = #PID
    • END-SELECT
    • INPUT (AD=M) EMP.PERSONNEL_ID EMP.NAME EMP.SALARY_1
    • UPDATE EMP SET *
    • WHERE PERSONNEL_ID = #PID
    • END-REPEAT
  • 57. Updating 2
    • DEFINE DATA LOCAL
    • 1 EMP VIEW OF I038-EMPLOYEES
    • 2 NAME
    • 2 PERSONNEL_ID
    • 2 SALARY_1
    • 1 #PID (A8)
    • END-DEFINE
    • REPEAT
    • INPUT #PID
    • SELECT * INTO VIEW EMP FROM I038-EMPLOYEES
    • WHERE PERSONNEL_ID = #PID
    • END-SELECT
    • INPUT (AD=M) EMP.PERSONNEL_ID EMP.NAME EMP.SALARY_1
    • UPDATE I038-EMPLOYEES SET
    • NAME = EMP.NAME, SALARY_1 = EMP.SALARY_1
    • WHERE PERSONNEL_ID = #PID
    • END-REPEAT
    • END
  • 58. Cross Transation Update
    • > TEST DBLOG Q START > + Program EMPUPD1 Lib DRSLIB
    • All ....+....1....+....2....+....3....+....4....+....5....+.Mode Structured.
    • 0010 DEFINE DATA LOCAL
    • 0020 1 EMP VIEW OF I038-EMPLOYEES
    • 0030 2 NAME
    • 0040 2 PERSONNEL_ID
    • 0050 2 SALARY_1
    • 0060 1 EMP2 VIEW OF I038-EMPLOYEES
    • 0070 2 NAME
    • 0080 2 PERSONNEL_ID
    • 0090 2 SALARY_1
    • 0100 1 #PID (A8)
    • 0110 END-DEFINE
    • 0120 INPUT #PID
    • 0130 RD1.
    • 0140 SELECT * INTO VIEW EMP FROM I038-EMPLOYEES
    • 0150 WHERE PERSONNEL_ID = #PID
    • 0160 INPUT (AD=M) EMP.PERSONNEL_ID EMP.NAME EMP.SALARY_1
    • 0170 UPDATE (RD1.)
    • 0180 END-SELECT
    • 0190 END
  • 59. Updating one record
    • #PID 30000100
  • 60. Updating one record
    • PERSONNEL_ID 30000100 NAME LLOYD SALARY_1 7200
  • 61. SQL Log from Update IMS with FS
    • User DRS - DBLOG Trace - Library DRSLIB
    • M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV
    • _ 1 SELECT NAME,PERSONNEL_ID,SALA 01 01 0130 D DB2 EMPUPD1 0130 01
    • _ 2 FETCH CURSOR 01 01 0130 D DB2 EMPUPD1 0130 01
    • _ 3 ROLLOUTS TO FILE SERVER:00000 01 01 0130 D DB2 EMPUPD1 0160 01
    • _ 4 * SELECT NAME,PERSONNEL_ID,SALA 01 01 0130 D DB2 EMPUPD1 0170 01
    • _ 5 * FETCH CURSOR 01 01 0130 D DB2 EMPUPD1 0170 01
    • _ 6 * UPDATE I038.EMPLOYEES SET NAM 01 02 0130 D DB2 EMPUPD1 0170 01
    • _ 7 * CLOSE CURSOR 01 01 0130 D DB2 EMPUPD1 0170 01
    • _ 8 READ FROM FILE SERVER 00 00 0130 D DB2 100 EMPUPD1 0130 01
    • _ 9 CLOSE FILE SERVER 00 00 0130 D DB2 EMPUPD1 0130 01
  • 62. SQL Log from Update - Complete
    • M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV
    • _ 1 SELECT NAME,PERSONNEL_ID FROM 01 01 0110 D DB2 EMPUPD 0110 01
    • _ 2 FETCH CURSOR NEX 01 01 0110 D DB2 EMPUPD 0110 01
    • _ 3 POSSIBLY IMPLICIT COMMIT 00 00 0000 D DB2 EMPUPD 0140 01
    • _ 4 UPDATE SADRS.EMPL_BASE SET NA 01 02 0110 D DB2 EMPUPD 0150 01
    • _ 5 FETCH CURSOR NEX 01 01 0110 D DB2 100 EMPUPD 0110 01
    • _ 6 CLOSE CURSOR 01 01 0110 D DB2 EMPUPD 0110 01
  • 63. Updating without reading row first
    • DEFINE DATA LOCAL
    • 1 #PID (A8)
    • END-DEFINE
    • UPDATE I038-EMPLOYEES SET
    • SALARY_1 = SALARY_1 + .05*SALARY_1
    • WHERE DEPT = 'COMP02'
    • END
  • 64. Sample 1 code SELECT * INTO VIEW FT_FINAN_TRAN FROM FT_FINAN_TRAN WHERE COY = #CCCWOTN-IN.COY AND TRAN_ACCOUNT_ID = #CCCWOTN-IN.ACCOUNT_ID AND TRAN_SA_ID = #CCCWOTN-IN.SA_ID ORDER BY TRAN_ACCOUNT_ID, TRAN_DT, ADD_TIME REJECT IF FT_FINAN_TRAN.TRAN_CANCELED = 'Y' REJECT IF FT_FINAN_TRAN.TRAN_MEMO_IND = 'Y' REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#MEMO REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#CASH-DIST REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#CASH-DIST-CANCEL-DETAIL REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#BILL-CANCEL-DETAIL REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#PAY-CANCEL-DETAIL REJECT IF (FT_FINAN_TRAN.TRAN_TOT_AMT = 0 AND FT_FINAN_TRAN.TRAN_WRITE_OFF = 0)
  • 65. Sample 1 DBLOG M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV _ 1 SELECT LOG_COUNTER,COY,ADD_US 01 01 2670 D DB2 DRSOYN5 2670 01 _ 2 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 3 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 4 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 5 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 6 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 7 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 8 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 9 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 10 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 11 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 12 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 13 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 14 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 15 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 ………………………………………………………………………………………………………………………………………………………… .. _ 28 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 29 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 30 FETCH CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01 _ 31 FETCH CURSOR 01 01 2670 D DB2 100 DRSOYN5 2670 01 _ 32 CLOSE CURSOR 01 01 2670 D DB2 DRSOYN5 2670 01
  • 66. Sample 1 Code Revision SELECT * INTO VIEW FT_FINAN_TRAN FROM FT_FINAN_TRAN WHERE COY = #CCCWOTN-IN.COY AND TRAN_ACCOUNT_ID = #CCCWOTN-IN.ACCOUNT_ID AND TRAN_SA_ID = #CCCWOTN-IN.SA_ID AND FT_FINAN_TRAN.TRAN_CANCELED <> 'Y' AND TRAN_MEMO_IND <> 'Y' AND TRAN_TYPE NOT IN (#CFTTRTL.#MEMO, #CFTTRTL.#CASH-DIST,#CFTTRTL.#CASH-DIST-CANCEL-DETAIL, #CFTTRTL.#BILL-CANCEL-DETAIL, #CFTTRTL.#PAY-CANCEL-DETAIL) AND TRAN_TOT_AMT + TRAN_WRITE_OFF <> 0 ORDER BY TRAN_ACCOUNT_ID, TRAN_DT, ADD_TIME *
  • 67. Sample 1 Revised DBLOG M No R SQL Statement (truncated) CU SN SREF M Typ SQLC/W Program Line LV _ 1 SELECT LOG_COUNTER,COY,ADD_US 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 2 FETCH CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 3 FETCH CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 4 FETCH CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 5 FETCH CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 6 FETCH CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 7 FETCH CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 8 FETCH CURSOR 01 01 2420 D DB2 100 DRSOYN5Z 2420 01 _ 9 CLOSE CURSOR 01 01 2420 D DB2 DRSOYN5Z 2420 01 _ 10 ROLLBACK 00 00 0000 D DB2 DBLOQ 0000 00
  • 68. Sample 2 SELECT * INTO VIEW CIS_ACCOUNT FROM CIS_ACCOUNT WHERE COY = #INPUT.COY AND ACCOUNT_ID = AD_ADJUST.ACCOUNT_ID OPTIMIZE FOR 1 ROW * SP. SELECT * INTO VIEW CIS_PERSON FROM CIS_PERSON WHERE COY = #INPUT.COY AND PERSON_ID = CIS_ACCOUNT.CUST_ID OPTIMIZE FOR 1 ROW *
  • 69. Sample 2 SELECT A.ACCOUNT_ID, A.CUST_ID, B.PERSON_ID, B.LAST_NAME, B.FIRST_NAME, B.BUSINESS_NAME INTO CIS_ACCOUNT.ACCOUNT_ID, CIS_ACCOUNT.CUST_ID ,CIS_PERSON.PERSON_ID, CIS_PERSON.LAST_NAME ,CIS_PERSON.FIRST_NAME, CIS_PERSON.BUSINESS_NAME FROM CIS_ACCOUNT A, CIS_PERSON B WHERE A.COY = #INPUT.COY AND A.ACCOUNT_ID = AD_ADJUST.ACCOUNT_ID AND B.COY = A.COY AND B.PERSON_ID = A.CUST_ID OPTIMIZE FOR 1 ROW
  • 70. Sample 2 Queryno 1 EXPLAIN Result Row 1/ 2 Estimated cost : 0.0 timerons Qblockno Mixop Access Match Index Pre- Access-Column- Planno seq type cols only fetch creator name fn_eval --- --- --- ---- ----- ----- ----- -------- ------------------- 1 1 I 2 CSDOWN21 COY_ACCOUNT_ID 1 2 I 2 CSDOWN21 PR_STD_FRMAT_NM Table Tslock -- sortn -- -- sortc -- Tabno Creator Tname mode Method uq jo or gr uq jo or gr --- -------- ------------------ ------ ------ -- -- -- -- -- -- -- -- 1 CSDOWN21 CIS_ACCOUNT_T IS N N N N N N N N 2 CSDOWN21 CIS_PERSON_T IS 1 N N N N N N N N 1 SELECT A.ACCOUNT_ID,A.CUST_ID 01 01 0880 D DB2 DRS011PX 0880 01 2 FETCH CURSOR 01 01 0880 D DB2 DRS011PX 0880 01 3 CLOSE CURSOR 01 01 0880 D DB2 DRS011PX 1230 01
  • 71. Methodology for making changes
    • Make a copy of Program
    • Leave LDA in place
    • If program is one easy to test, put display before Find/READ/SELECT statement of all variables used in search. Record values
    • Change type to program if necessary make parameter data areas be local if it was a subprogram.
    • Eliminate all code except DATA AREAS and Search .
    • Add assignment of variables used in search.
    • Add display of selected values.
    • Run program using DBLOG.
    • Do and explain.
    • Make code changes and repeat steps 7 and 8
  • 72. Methodology continued Before
    • DEFINE DATA PARAMETER
    • … ..
    • LOCAL
    • … .
    • … .
    • END-DEFINE
    • … .
    • … .
    • … .
    • FIND
    • FIND
    • …… .
    • …… .
    • END-FIND
    • … ..
    • END-FIND
    • ……
    • … ..
    • END
  • 73. Methodology continued First Step
    • DEFINE DATA LOCAL
    • … ..
    • LOCAL
    • … .
    • … .
    • END-DEFINE
    • ASSIGN ……..
    • FIND
    • FIND
    • DISPLAY …..
    • End-Find
    • End-find
  • 74. Methodology continued Second Step
    • DEFINE DATA LOCAL
    • … ..
    • LOCAL
    • … .
    • … .
    • END-DEFINE
    • ASSIGN ……..
    • SELECT
    • DISPLAY …..
    • END-SELECT
    • END
  • 75. Sample 1 code Before SELECT * INTO VIEW FT_FINAN_TRAN FROM FT_FINAN_TRAN WHERE COY = #CCCWOTN-IN.COY AND TRAN_ACCOUNT_ID = #CCCWOTN-IN.ACCOUNT_ID AND TRAN_SA_ID = #CCCWOTN-IN.SA_ID ORDER BY TRAN_ACCOUNT_ID, TRAN_DT, ADD_TIME REJECT IF FT_FINAN_TRAN.TRAN_CANCELED = 'Y' REJECT IF FT_FINAN_TRAN.TRAN_MEMO_IND = 'Y' REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#MEMO REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#CASH-DIST REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#CASH-DIST-CANCEL-DETAIL REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#BILL-CANCEL-DETAIL REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#PAY-CANCEL-DETAIL REJECT IF (FT_FINAN_TRAN.TRAN_TOT_AMT = 0 AND FT_FINAN_TRAN.TRAN_WRITE_OFF = 0)
  • 76. Sample 1 code Methodology continued First Step ASSIGN #CCCWOTN-IN.COY = xx ASSIGN #CCCWOTN-IN.ACCOUNT_ID =xxxxxxx ASSIGN #CCCWOTN-IN.SA_ID = yyyyyyyy SELECT * INTO VIEW FT_FINAN_TRAN FROM FT_FINAN_TRAN WHERE COY = #CCCWOTN-IN.COY AND TRAN_ACCOUNT_ID = #CCCWOTN-IN.ACCOUNT_ID AND TRAN_SA_ID = #CCCWOTN-IN.SA_ID ORDER BY TRAN_ACCOUNT_ID, TRAN_DT, ADD_TIME REJECT IF FT_FINAN_TRAN.TRAN_CANCELED = 'Y' REJECT IF FT_FINAN_TRAN.TRAN_MEMO_IND = 'Y' REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#MEMO REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#CASH-DIST REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#CASH-DIST-CANCEL-DETAIL REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#BILL-CANCEL-DETAIL REJECT IF FT_FINAN_TRAN.TRAN_TYPE = #CFTTRTL.#PAY-CANCEL-DETAIL REJECT IF (FT_FINAN_TRAN.TRAN_TOT_AMT = 0 AND FT_FINAN_TRAN.TRAN_WRITE_OFF = 0 DISPLAY FT_FINAN_TRAN.TRAN_TYPE FT_FINAN_TRAN.TRAN_TOT_AMT
  • 77. Sample 1 code Methodology continued Second Step ASSIGN #CCCWOTN-IN.COY = xx ASSIGN #CCCWOTN-IN.ACCOUNT_ID =xxxxxxx ASSIGN #CCCWOTN-IN.SA_ID = yyyyyyyy SELECT * INTO VIEW FT_FINAN_TRAN FROM FT_FINAN_TRAN WHERE COY = #CCCWOTN-IN.COY AND TRAN_ACCOUNT_ID = #CCCWOTN-IN.ACCOUNT_ID AND TRAN_SA_ID = #CCCWOTN-IN.SA_ID AND FT_FINAN_TRAN.TRAN_CANCELED <> 'Y' AND TRAN_MEMO_IND <> 'Y' AND TRAN_TYPE NOT IN (#CFTTRTL.#MEMO, #CFTTRTL.#CASH-DIST,#CFTTRTL.#CASH-DIST-CANCEL-DETAIL, #CFTTRTL.#BILL-CANCEL-DETAIL, #CFTTRTL.#PAY-CANCEL-DETAIL) AND TRAN_TOT_AMT + TRAN_WRITE_OFF <> 0 ORDER BY TRAN_ACCOUNT_ID, TRAN_DT, ADD_TIME DISPLAY FT_FINAN_TRAN.TRAN_TYPE FT_FINAN_TRAN.TRAN_TOT_AMT
  • 78. Natural for DB2 4.2 - News Software AG
  • 79. Contents
    • DB2 UDB for z/OS
    • Version 8 Support
    • Unicode Support
  • 80. Dynamic Scrollable Cursors I ASENSITIVE SENSITIVE DYNAMIC STATIC SCROLLABLE NOT SCROLLABLE INSENSITIVE DB2 UDB for z/OS Version 8 CURSORS Cursor sensitivity - the degree to which database updates are visible to the subsequent FETCH statements in a cursor. These changes can be made by this or by other application process (UOW).
  • 81. Dynamic Scrollable Cursors II DB2 Temporary Database FETCH INSENSITIVE SENSITIVE STATIC INSENSITIVE FETCH SENSITIVE No checks against Base Table Is checked against Base Table: Holes (SQLCODE)
  • 82. Dynamic Scrollable Cursors III > > + Program DEM2SCRL Lib SYSDB2 ....+....1....+....2....+....3....+....4....+....5....+....6....+....7.. 0380 MOVE 'SENSITIVE ABSOLUTE + 1' to SCROLL_HV 0390 SELECT * 0400 INTO VIEW NAT-VIEW 0410 FROM NAT-DEMO 0420 WHERE SALARY BETWEEN :SALARY_LOW AND :SALARY_HIGH 0430 ORDER BY NAME 0440 WITH SENSITIVE STATIC SCROLL :SCROLL_HV GIVING :SQLCODE 0450 DECIDE ON FIRST VALUES OF SQLCODE 0460 VALUES 100 [:]  scroll_hv  [GIVING [:]  sqlcode SELECT  selection   … ASENSITIVE SCROLL INSENSITIVE SCROLL SENSITIVE STATIC SCROLL SENSITIVE DYNAMIC SCROLL   WITH DB2: SQLCODE=222 Update / Delete hole encountered
  • 83. Sequence Objects and Identify Columns I CREATE SEQUENCE ORDER_SEQ START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE CACHE 20; INSERT INTO ORDERS (ORDERNO, CUSTNO) VALUES ( NEXT VALUE FOR ORDER_SEQ , 123456); A sequence is a user-defined object that generates a sequence of numeric values
  • 84. Sequence Objects Identify Columns II 0070 PROCESS SQL SYSIBM-SYSDATABASE 0080 << CREATE SEQUENCE TEST.ORDER_SEQ START WITH 1 CACHE 20 >> 0090 SELECT 0100 NEXT VALUE FOR TEST.ORDER_SEQ 0110 INTO :NEXTVAL 0120 FROM SYSIBM-SYSDATABASE 0130 DISPLAY NEXTVAL PREVVAL 0140 END-SELECT 0150 SELECT 0160 NEXT VALUE FOR TEST.ORDER_SEQ , 0170 PREVIOUS VALUE FOR TEST.ORDER_SEQ 0180 INTO :NEXTVAL, :PREVVAL 0190 FROM SYSIBM-SYSDATABASE 0200 DISPLAY NEXTVAL PREVVAL 0210 END-SELECT Natural DB2: PROCESS SQL, SELECT, INSERT etc.
  • 85. Multiple Row Processing I Single Row Multi Row FETCH EXEC SQL DECLARE CURS1 CURSOR WITH ROWSET POSITIONING FOR SELECT…; FETCH ROWSET STARTING AT ABSOLUTE 10 FROM CURS1 FOR 6 ROWS INTO :hva1, :hva2, :hva3; A rowset is a set of rows for which a cursor position is established
  • 86. Multiple Row Processing II 0210 MOVE 25 TO MF 0220 FIND MULTI-FETCH OF MF VDEMO 0230 WITH EMPNO = 000000 THRU 999999 0240 WRITE VDEMO 0250 END-FIND Natural DML Natural SQL Natural DB2 Views Views and Host Variables (arrays) Natural DB2: support for SELECT and INSERT
  • 87. Multiple Row Processing III 0120 DEFINE DATA LOCAL 0130 01 NAME (A20/1:10) 0140 01 ADDRESS (A100/1:10) 0160 01 SALARY (P4.2/1:10) 0170 01 L§ADDRESS (I2/1:10) 0180 01 ROWS (I4) 0190 01 NUMBER (I4) 0200 01 INDEX (I4) 0210 END-DEFINE 0220 OPTIONS DB2ARRY=ON /* <-- ENABLE DB2 ARRAY 0250 SELECT NAME, ADDRESS , SALARY 0260 INTO :NAME(*), /* <-- ARRAY 0270 :ADDRESS(*) LINDICATOR :L§ADDRESS(*), /* <-- ARRAY 0290 :SALARY(01:10) /* <-- ARRAY 0300 FROM NAT-DEMO 0310 WHERE NAME > ' ' 0320 WITH ROWSET POSITIONING FOR 10 ROWS /* <-- ROWS REQ 0340 ROWS_RETURNED :ROWS /* <-- ROWS RET 0350 IF ROWS > 0 0360 FOR INDEX = 1 TO ROWS STEP 1 0370 DISPLAY 0390 NAME(INDEX) 0400 ADDRESS(INDEX) (AL=20) 0420 SALARY(INDEX) 0430 END-FOR 0440 END-IF 0450 END-SELECT
  • 88. Multiple Row Processing IV 0450 OPTIONS DB2ARRY=ON /* <-- ENABLE DB2 ARRAY 0470 INSERT INTO NAT-DEMO_ID 0480 (NAME,ADDRESS,DATEOFBIRTH,SALARY) 0490 VALUES 0500 (:NAME(*), /* <-- ARRAY 0510 :ADDRESS(*) /* <-- ARRAY 0520 INDICATOR :N§ADDRESS(*) /* <-- ARRAY 0530 LINDICATOR :L§ADDRESS(*), /* <-- ARRAY DB2 VCHAR 0550 :DATENATD(1:10), /* <-- ARRAY NATURAL DATES 0560 :SALARY(01:10) /* <-- ARRAY NATURAL PACKED 0580 ) 0590 FOR 10 ROWS 0610 * ATOMIC /* <-- DEFAULT OPTION 0620 NOT ATOMIC CONTINUE ON SQLEXCEPTION /* <-- SPECIAL OPTION ATOMIC / NOT ATOMIC ... – specifies the DB2 reaction on an INSERT error
  • 89. GET DIAGNOSTICS long rcount; EXEC SQL UPDATE T1 SET C1 = C1 + 1; EXEC SQL GET DIAGNOSTICS :rcount = ROW_COUNT; 1120 PROCESS SQL NAT-DEMO 1130 << GET DIAGNOSTICS CONDITION :U:NUMBER 1220 ,:G:MESSAGE_ID = MESSAGE_ID 1230 ,:G:MODULE = DB2_MODULE_DETECTING_ERROR 1300 ,:G:REASON = DB2_REASON_CODE 1310 ,:G:SQLCODE = DB2_RETURNED_SQLCODE 1320 ,:G:ROW_NUMBER = DB2_ROW_NUMBER 1330 ,:G:TOKEN_COUNT = DB2_TOKEN_COUNT 1340 ,:G:MESSAGE_TEXT = MESSAGE_TEXT 1350 ,:G:SQLSTATE = RETURNED_SQLSTATE 1360 ,:G:SERVER = SERVER_NAME 1370 >> GET DIAGNOSTICS provides diagnostic information about the last executed SQL statement
  • 90. XML Publishing Built-In Functions I DB2 XML Functions XMLELEMENT Generates an XML element from arguments XMLATTRIBUTES Constructs XML attributes from arguments XMLFOREST Produces a forest of XML elements from a list of columns and expressions XML2CLOB Converts the transient XML data type into a CLOB so that the application can access the XML data … XML Built-In Functions - a set of SQL built-in functions that allow applications to generate XML data from relational data.
  • 91. XML Publishing Built-In Functions II SELECT E.EMPNO, XML2CLOB ( XMLELEMENT ( NAME &quot;Emp&quot;, XMLATTRIBUTES ( E.FIRSTNME !! ' ' !! E.LASTNAME AS &quot;name&quot; ), XMLFOREST ( E.HIRE, E.WORKDEPT AS &quot;department&quot; ) ) ) FROM DSN8810.EMP E; ID result --------------------------------------------- 1001 <Emp name=&quot;John Smith&quot;> <HIRE>2000-05-24</HIRE> <department>Accounting</department> </Emp> 1001 <Emp name=&quot;Mary Martin&quot;> <HIRE>1996-02-01</HIRE> <department>Shipping</department> </Emp> Example: Generate an &quot;Emp&quot; element for each employee. Use employee name as its attribute and two subelements generated from columns HIRE and DEPT by using XMLFOREST as its content. The element names for the two subelements are &quot;HIRE&quot; and &quot;department&quot;.
  • 92. XML Publishing Built-In Functions III 0040 DEFINE DATA LOCAL 0050 01 #EMPNO (A6) 0060 01 #XML_LINE (A) DYNAMIC 0070 END-DEFINE 0080 SELECT E.EMPNO, XML2CLOB ( XMLELEMENT 0090 ( NAME &quot;EMP&quot;, 0100 XMLATTRIBUTES ( E.FIRSTNME !! ' ' !! E.LASTNAME 0110 AS &quot;NAME&quot; ), 0120 XMLFOREST ( E.HIREDATE, 0130 E.WORKDEPT AS &quot;DEPARTMENT&quot; ) 0140 ) ) AS RESULT 0150 INTO #EMPNO, #XML_LINE 0160 FROM DSN8810-EMP E; 0170 PRINT #EMPNO #XML_LINE ;
  • 93. Unicode Support I DB2 Unicode GRAPHIC Data type UNICODE, 1200 CCSID DB2 does not have data type ‘UNICODE’
  • 94. Unicode Support II 0090 DEFINE DATA LOCAL 0100 01 EMP VIEW OF DSN8810-EMP 0110 02 EMPNO 0130 02 LASTNAME 0140 02 SALARY 0170 * 0180 01 UC 0190 02 EMPNO (U6) 0200 02 FIRSTNME (U12) 0210 02 LASTNAME (U15) 0220 02 SALARY (P7.2) 0250 END-DEFINE 0290 * ALPHA 0350 SELECT * INTO VIEW EMP 0360 FROM DSN8810-EMP 0370 ORDER BY SALARY + COMM 0380 FETCH FIRST 50 ROW ONLY 0400 DISPLAY EMP *COUNTER 0410 END-SELECT 0290 * UNICODE 0480 SELECT EMPNO,FIRSTNME,SALARY, 0490 INTO UC.EMPNO,UC.FIRSTNME,UC.SALARY 0500 FROM DSN8810-EMP 0510 ORDER BY SALARY + COMM 0520 FETCH FIRST 50 ROW ONLY 0540 DISPLAY UC *COUNTER 0550 END-SELECT Natural DB2 uses the DB2 Code Page Translation (UTF16, Code Page 1200)
  • 95. SELECT Statement I INSERT SELECT CREATE TABLE EMPLOYEE (EMPNO INTEGER GENERATED ALWAYS AS IDENTITY , ID SMALLINT, NAME CHAR(30), SALARY DECIMAL(5,2), DEPTNO SMALLINT) IN DSN8D81A.DSN8S81D; SELECT EMPNO FROM FINAL TABLE (INSERT INTO DSN8810.EMP (NAME,SALARY,DEPTNO) VALUES(‘SMITH’,99000,123) ); DB2: I( insert )-table-reference
  • 96. SELECT Statement II 0160 SELECT * INTO VIEW V1 0170 FROM FINAL TABLE ( INSERT INTO NAT-DEMO_ID 0180 (NAME, ADDRESS,DATEOFBIRTH,SALARY) 0190 VALUES ('KLUGE','ANGLE STREET 1', 0200 '1980-05-05', 5720.05) 0210 ) 0220 DISPLAY (AL=15) V1 0230 END-SELECT
  • 97. Materialized Query Tables CREATE TABLE STRANS AS (SELECT YEAR AS SYEAR, MONTH AS SMONTH, DAY AS SDAY, SUM(AMOUNT) AS SSUM FROM TRANS GROUP BY YEAR, MONTH, DAY) DATA INITIALLY DEFERRED REFRESH DEFERRED ; MQT Population User-Maintained System-Maintained INSERT, UPDATE, DELETE REFRESH TABLE LOAD REFRESH TABLE 0940 PROCESS SQL SYSIBM-SYSDATABASE 0950 << REFRESH TABLE ‘EMPLOYEE’; >> A materialized query table (MQT) is a table whose definition is based on the result of a query.
  • 98. Current Package Path Register I PACKAGE NAME location.collection.DBRM SET CURRENT PACKAGESET Search for Collection SET CURRENT PACKAGE PATH In releases of DB2 before Version 8, the only way to switch between packages was to execute the SET CURRENT PACKAGESET statement every time you needed to use a different package. With SET CURRENT PACKAGE PATH , you can execute the statement only once, to give the server a list of package collections to search. before DB2 Version 8 DB2 Version 8
  • 99. Current Package Path Register II 0940 PROCESS SQL SYSIBM-SYSDUMMY1 0950 << SET CURRENT PACKAGE PATH = SYSIBM, ‘ADMIN’, ‘USER’ >> 14:54:32 ***** NATURAL TOOLS FOR DB2 ***** 2006-02-10 - Environment Setting - Code Function Code Function SET CURRENT CO CONNECT SS SQLID RE RELEASE (connection) SP PACKAGESET SC SET CONNECTION SD DEGREE SY SET ENCRYPTION PASSWORD SU RULES SR Display SPECIAL REGISTER SO OPTIMIZATION HINT ? Help SL LOCALE LC_CTYPE . Exit SA PATH SE PRECISION SM MAINTAINED TABLE TYPES FOR OPT SB PACKAGE PATH Code .. __ SF REFRESH AGE SH SCHEMA SN APPLICATION ENCODING SCHEME Natural DB2: PROCESS SQL, SYSDB2
  • 100. Common Table Expressions I Table Expressions Nested SELECT … FROM ( SELECT EDLEVEL, YEAR(HIREDATE) AS HIREYEAR, SALARY+BONUS+COMM AS TOTAL_PAY FROM EMPLOYEE WHERE EDLEVEL > 16 ) WITH PAYLEVEL AS ( SELECT EMPNO, EDLEVEL, YEAR(HIREDATE) AS HIREYEAR, SALARY+BONUS+COMM AS TOTAL_PAY FROM EMPLOYEE WHERE EDLEVEL > 16 ) SELECT … FROM PAYLEVEL Common Table A common table expression permits defining a result table with a table name that can be referenced as a table name in any FROM clause of the fullselect that follows.
  • 101. Common Table Expressions II 0270 WITH_CTE 0280 RPL (PART,SUBPART,QUANTITY) AS 0290 (SELECT ROOT.PART,ROOT.SUBPART,ROOT.QUANTITY 0300 FROM HGK-PARTLIST ROOT (PART,SUBPART,QUANTITY) 0310 WHERE ROOT.PART ='01' 0320 UNION ALL 0330 SELECT CHILD.PART,CHILD.SUBPART,CHILD.QUANTITY 0340 FROM RPL PARENT, HGK-PARTLIST CHILD 0350 WHERE PARENT.SUBPART =CHILD.PART 0360 ) 0370 SELECT DISTINCT PART,SUBPART,QUANTITY 0380 INTO VIEW V1 0390 FROM RPL 0400 ORDER BY PART,SUBPART,QUANTITY 0410 PERFORM CHECK-SQLCA 0420 DISPLAY *COUNTER V1 0430 END-SELECT Natural DB2: WITH_CTE and subsequent SELECT
  • 102.