home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0210 - 0219 / ibm0210-0219 / ibm0213.tar / ibm0213 / 7025PWA1.ZIP / EXPLAIN.ZIP / exp.cmd next >
Encoding:
Text File  |  1994-02-28  |  3.5 KB  |  137 lines

  1. /* A REXX Driver to invoke the EXPLAIN tool for all plans in
  2.    a specified database */
  3.  
  4. /* Change the SELECT statement by adding predicates (WHERE clause) if
  5.    you only want a selective set of access plans.  However, this program
  6.    is dependent on the SELECT list and FROM clause remaining constant */
  7.  
  8. stmt = 'SELECT creator, name FROM sysibm.sysplan ORDER BY 1, 2'
  9.  
  10. /* Register SQLDBS and SQLEXEC external entry points */
  11. if rxfuncquery('SQLDBS') <> 0 then do
  12.   rcy = rxfuncadd('SQLDBS', 'SQLAR', 'SQLDBS')
  13.   if rcy <> 0 then
  14.     say 'RxFuncAdd return code for SQLDBS is' rcy
  15. end
  16.  
  17. if rxfuncquery('SQLEXEC') <> 0 then do
  18.   rcz = rxfuncadd('SQLEXEC', 'SQLAR', 'SQLEXEC')
  19.   if rcz <> 0 then
  20.     say 'RxFuncAdd return code for SQLEXEC is' rcz
  21. end
  22.  
  23. say
  24. say 'Database Name'
  25. pull dbname
  26. call sqldbs 'Start Using Database' dbname
  27.  
  28. /* display any error messages */
  29. sql_result = check_sql()
  30.  
  31. if sql_result = 0 then do
  32.  
  33.   /* declare cursor for select statements */
  34.   call sqlexec declare c51 cursor with hold for s51
  35.  
  36.   /* display any error messages */
  37.   sql_result = check_sql()
  38.  
  39.   if sql_result = 0 then do
  40.  
  41.     /* call sqlexec to prepare the sql statement */
  42.     call sqlexec prepare s51 from ':stmt'
  43.  
  44.     /* display any error messages */
  45.     sql_result = check_sql()
  46.  
  47.     if sql_result = 0 then do
  48.  
  49.       /* call sqlexec to open the cursor */
  50.       call sqlexec open c51
  51.  
  52.       /* display any error messages */
  53.       sql_result = check_sql()
  54.  
  55.       count = 0
  56.  
  57.       /* while no sql errors */
  58.       do while sql_result = 0
  59.  
  60.         /* call sqlexec to fetch a row of data */
  61.         call sqlexec fetch c51 into ':creator, :planname'
  62.  
  63.         /* display any error messages */
  64.         sql_result = check_sql()
  65.  
  66.         /* if successful fetch */
  67.         if sql_result = 0 then do
  68.  
  69.           count = count + 1
  70.  
  71.           /* get rid of trailing blanks */
  72.           planname = strip(planname, T, ' ')
  73.           creator  = strip(creator,  T, ' ')
  74.  
  75.           /* if first invocation of explain */
  76.           if count = 1 then do
  77.  
  78.             /* this prevents lock contention if the explain plan has
  79.                to be bound */
  80.             call sqlexec commit
  81.  
  82.             /* display any error messages */
  83.             sql_result = check_sql()
  84.  
  85.           end
  86.  
  87.           if sql_result = 0 then do
  88.  
  89.             /* call explain */
  90.             explain dbname planname creator 0 planname'.EXP'
  91.  
  92.           end
  93.         end
  94.         else do
  95.           if sql_result = 100 & count = 0 then do
  96.             parse source with 'COMMAND ' src
  97.             say 'No plans were found in database' dbname
  98.             say 'which match the SQL SELECT statement in' src
  99.           end
  100.         end
  101.       end
  102.  
  103.       /* end-of-file */
  104.       if sql_result = 100 then do
  105.  
  106.         /* call sqlexec to close the cursor */
  107.         call sqlexec close c51
  108.  
  109.         /* display any error messages */
  110.         sql_result = check_sql()
  111.       end
  112.     end
  113.   end
  114.  
  115.   call sqldbs stop using database
  116.   sql_result = check_sql()
  117.  
  118.   /* drop the SQLDBS and SQLEXEC external functions */
  119.   rcy = rxfuncdrop('SQLDBS')
  120.   rcz = rxfuncdrop('SQLEXEC')
  121. end
  122. exit sql_result
  123.  
  124.  
  125.  
  126. check_sql: procedure expose result sqlca. sqlmsg
  127.   if (result <> 0) then do
  128.     sql_result = result
  129.     say 'Result =' result
  130.   end
  131.   else do
  132.     sql_result = sqlca.sqlcode
  133.     if sqlca.sqlcode <> 0 & sqlca.sqlcode <> 100 then
  134.       say sqlmsg
  135.   end
  136. return sql_result
  137.