Some New Features of DB2 v7 Pam Odden
Objectives <ul><li>To try out some new SQL supported in DB2 v7 </li></ul><ul><ul><li>Order by using an expression </li></u...
ORDER BY Using an Expression <ul><li>A new option, ORDER BY  expression , lets you specify an expression with operators as...
Using a UNION in a View <ul><li>Version 7 adds support for the UNION and UNION ALL operators in a view definition.  </li><...
Self-referencing Subselect in Searched Update  <ul><li>In previous releases of DB2, in a searched UPDATE and DELETE statem...
Declared Temporary Table <ul><li>Declare in one of three ways </li></ul><ul><ul><li>Specify the columns </li></ul></ul><ul...
FETCH FIRST n ROWS ONLY Clause <ul><li>This clause actually limits the number of rows that can be fetched. </li></ul><ul><...
FETCH FIRST n ROWS ONLY Clause <ul><li>*CURSOR TO RETRIEVE FIRST N ROWS  </li></ul><ul><li>EXEC SQL  </li></ul><ul><li>DEC...
Schedule <ul><li>During the next 2 weeks, use the following STEPLIB statements to run IKJEFTIA (TSO terminal monitor progr...
Upcoming SlideShare
Loading in …5
×

Some New Features of DB2 v7

519 views
452 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
519
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • We ordered SQL Ref and App Dev guide (2 bks) for each team and Java and SQL quick reference for everyone.
  • Samples are in sysad2.sql(db2v7) In the example, could have ordered by 3 in v5 to get around the limitation. Had to select it though. I have never thought Gee, I wish I could order by an expression. There are many rules associated with the use of expressions in order by. In Grouped queries, unions, etc.
  • SYSAD2.SQL (practice)
  • Note- I first declared the temp table in the working storage part of the program. Had to move it to procedure division.
  • Run PJO.COBOL(SAMPLE5) with and without commented out fetch clause Use PJO.JCL(test5)
  • Terminal monitor pgm runs our own programs that use db2
  • Some New Features of DB2 v7

    1. 1. Some New Features of DB2 v7 Pam Odden
    2. 2. Objectives <ul><li>To try out some new SQL supported in DB2 v7 </li></ul><ul><ul><li>Order by using an expression </li></ul></ul><ul><ul><li>Using a union in a view </li></ul></ul><ul><ul><li>Self-referencing subselect in a searched update or delete </li></ul></ul><ul><ul><li>Declared temporary tables </li></ul></ul><ul><ul><li>Fetch first n rows only </li></ul></ul>
    3. 3. ORDER BY Using an Expression <ul><li>A new option, ORDER BY expression , lets you specify an expression with operators as the sort key for the result table of a SELECT statement </li></ul><ul><li>SELECT FIRSTNAME, CUMMGPA1, CUMMGPA1 * 100 FROM SSASIDB1.ASTU_STUDENT </li></ul><ul><li>WHERE SCHOOLNUM = '945' </li></ul><ul><li>AND CUMMGPA1 > 0 </li></ul><ul><li>ORDER BY CUMMGPA1 * 100; </li></ul><ul><li>Result in v5 </li></ul><ul><li>---------+---------+---------+---------+---------+---------+---------+--------- </li></ul><ul><li>DSNT408I SQLCODE = -104, ERROR: ILLEGAL SYMBOL &quot;*&quot;. SOME SYMBOLS THAT MIGHT BE </li></ul><ul><li>LEGAL ARE: , FOR WITH OPTIMIZE </li></ul><ul><li>Result in v7 </li></ul><ul><li>FIRSTNAME CUMMGPA1 </li></ul><ul><li>---------+---------+---------+---------+---------+---------+---------+--------- </li></ul><ul><li>MEAGAN JEAN .0556 5.5600 </li></ul><ul><li>ROBERT .0833 8.3300 </li></ul><ul><li>AMALIE .1538 15.3800 </li></ul><ul><li>MICHELLE .1667 16.6700 </li></ul>
    4. 4. Using a UNION in a View <ul><li>Version 7 adds support for the UNION and UNION ALL operators in a view definition. </li></ul><ul><li>This enhancement increases programmer productivity by allowing complex unions in views, rather than in each cursor. </li></ul><ul><li>SELECT MAX(EFFDATE) FROM </li></ul><ul><li>( SELECT PERMNUM, ENTERCODE, LEAVECODE, EFFDATE </li></ul><ul><li>FROM SSASIDB1.AENR_HISTORY </li></ul><ul><li>WHERE PERMNUM = ' 400004' </li></ul><ul><li>UNION </li></ul><ul><li>SELECT PERMNUM, ENTERCODE, LEAVECODE, ENTERDATE AS EFFDATE </li></ul><ul><li>FROM SSASIDB1.ASTU_STUDENT </li></ul><ul><li>WHERE PERMNUM = ' 400004' ) </li></ul><ul><li>AS STUD_HISTORY; </li></ul><ul><li>Result in v5 </li></ul><ul><li>DSNT408I SQLCODE = -199, ERROR: ILLEGAL USE OF KEYWORD UNION, TOKEN ) WAS EXPECTED </li></ul><ul><li>Result in v7 </li></ul><ul><li>------+---------+---------+---------+---------+---------+------ </li></ul><ul><li>08/25/2003 </li></ul>
    5. 5. Self-referencing Subselect in Searched Update <ul><li>In previous releases of DB2, in a searched UPDATE and DELETE statement, the WHERE clause cannot refer to the object being modified by the statement. Version 7 removes the restriction for the searched UPDATE and DELETE statements, but not for the positioned UPDATE and DELETE statements. </li></ul><ul><li>UPDATE PJO.DEMO2 </li></ul><ul><li>SET CREDEARNED = CREDEARNED + 10 </li></ul><ul><li>WHERE ENTERDATE = '01/28/2002' </li></ul><ul><li>AND CREDEARNED < </li></ul><ul><li>( SELECT AVG(CREDEARNED) FROM PJO.DEMO2 ); </li></ul><ul><li>Result in v5 </li></ul><ul><li>DSNT408I SQLCODE = -118, ERROR: THE OBJECT TABLE OR VIEW OF THE INSERT, DELETE, OR UPDATE STATEMENT IS ALSO IDENTIFIED IN A FROM CLAUSE </li></ul><ul><li>Result in v7 </li></ul><ul><li>---------+---------+---------+---------+---------+--------- </li></ul><ul><li>DSNE615I NUMBER OF ROWS AFFECTED IS 82 </li></ul><ul><li>DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 </li></ul>
    6. 6. Declared Temporary Table <ul><li>Declare in one of three ways </li></ul><ul><ul><li>Specify the columns </li></ul></ul><ul><ul><li>Use a LIKE clause to copy the definition of a base table, created temporary table, or view. </li></ul></ul><ul><ul><li>Use a select statement to choose specific columns from a base table, created temporary table, or view. </li></ul></ul><ul><li>Populate the declared temporary table using INSERT statements, modify the table using searched or positioned UPDATE or DELETE statements, and query the table using SELECT statements. </li></ul><ul><li>You can also create indexes on the declared temporary table. </li></ul><ul><li>The qualifier, SESSION, must be specified in any statement that references the table </li></ul>
    7. 7. FETCH FIRST n ROWS ONLY Clause <ul><li>This clause actually limits the number of rows that can be fetched. </li></ul><ul><li>An attempt to fetch n + 1 rows is handled the same way as normal end of data, with SQL code +100. </li></ul><ul><li>This improves the performance of queries with potentially large result sets when only a limited number of rows are needed. </li></ul><ul><li>This could be used as in the example below to fill an online screen: </li></ul><ul><ul><ul><li>SELECT LASTNAME, FIRSTNAME, EMPNO, SALARY </li></ul></ul></ul><ul><ul><ul><li>FROM EMPLOYEE </li></ul></ul></ul><ul><ul><ul><li>WHERE EMPNO > :HV-LAST-EMPNO </li></ul></ul></ul><ul><ul><ul><li>ORDER BY SALARY DESC </li></ul></ul></ul><ul><ul><ul><li>FETCH FIRST 20 ROWS ONLY; </li></ul></ul></ul><ul><li>You can specify FETCH FIRST ROW ONLY on a SELECT INTO statement when the query can return more than one row in the answer set. This tells DB2 that you are only interested in the first row, and allows a singleton select instead of a cursor. </li></ul>
    8. 8. FETCH FIRST n ROWS ONLY Clause <ul><li>*CURSOR TO RETRIEVE FIRST N ROWS </li></ul><ul><li>EXEC SQL </li></ul><ul><li>DECLARE CURS-FIRST-ROWS CURSOR FOR </li></ul><ul><li>SELECT FIRSTNAME, </li></ul><ul><li>ENTERCODE, </li></ul><ul><li>ENTERDATE, </li></ul><ul><li>GRADE </li></ul><ul><li>FROM PJO.DEMO2 </li></ul><ul><li>WHERE ENTERDATE = '01/28/2002' </li></ul><ul><li>* FETCH FIRST 20 ROWS ONLY </li></ul><ul><li>FETCH FIRST ROW ONLY </li></ul><ul><li>END-EXEC. </li></ul><ul><li>Run with FETCH FIRST clauses commented out: </li></ul><ul><li>ROWS FETCHED = 000000105 </li></ul><ul><li>Run with FETCH FIRST 20 ROWS ONLY: </li></ul><ul><li>ROWS FETCHED = 000000020 </li></ul><ul><li>Run with FETCH FIRST ROW ONLY: </li></ul><ul><li>ROWS FETCHED = 000000001 </li></ul>
    9. 9. Schedule <ul><li>During the next 2 weeks, use the following STEPLIB statements to run IKJEFTIA (TSO terminal monitor program), DSN1COPY (to restore TSN or BSN from a DSN image copy), or batch spufi commands </li></ul><ul><li>//STRTU00 EXEC PGM=IKJEFT1A,DYNAMNBR=20,COND=(4,LT) </li></ul><ul><li>//STEPLIB DD DSN=SYS2.DSN710.SDSNLOAD,DISP=SHR </li></ul><ul><li>// DD DSN=SYS2.DSN710.TEST.SDSNEXIT,DISP=SHR </li></ul><ul><li>Continue to use TSO logon procs TSODB2T and TSODB2B when you want to use SPUFI or other DB2 online </li></ul><ul><li>New version of PRF will be installed, meanwhile old version can be used in TSN/BSN using new logon procs </li></ul><ul><li>New File-AID for DB2 and Expeditor have been ordered </li></ul>

    ×