home *** CD-ROM | disk | FTP | other *** search
- /* A REXX Driver to invoke the EXPLAIN tool for all plans in
- a specified database */
-
- /* Change the SELECT statement by adding predicates (WHERE clause) if
- you only want a selective set of access plans. However, this program
- is dependent on the SELECT list and FROM clause remaining constant */
-
- stmt = 'SELECT creator, name FROM sysibm.sysplan ORDER BY 1, 2'
-
- /* Register SQLDBS and SQLEXEC external entry points */
- if rxfuncquery('SQLDBS') <> 0 then do
- rcy = rxfuncadd('SQLDBS', 'SQLAR', 'SQLDBS')
- if rcy <> 0 then
- say 'RxFuncAdd return code for SQLDBS is' rcy
- end
-
- if rxfuncquery('SQLEXEC') <> 0 then do
- rcz = rxfuncadd('SQLEXEC', 'SQLAR', 'SQLEXEC')
- if rcz <> 0 then
- say 'RxFuncAdd return code for SQLEXEC is' rcz
- end
-
- say
- say 'Database Name'
- pull dbname
- call sqldbs 'Start Using Database' dbname
-
- /* display any error messages */
- sql_result = check_sql()
-
- if sql_result = 0 then do
-
- /* declare cursor for select statements */
- call sqlexec declare c51 cursor with hold for s51
-
- /* display any error messages */
- sql_result = check_sql()
-
- if sql_result = 0 then do
-
- /* call sqlexec to prepare the sql statement */
- call sqlexec prepare s51 from ':stmt'
-
- /* display any error messages */
- sql_result = check_sql()
-
- if sql_result = 0 then do
-
- /* call sqlexec to open the cursor */
- call sqlexec open c51
-
- /* display any error messages */
- sql_result = check_sql()
-
- count = 0
-
- /* while no sql errors */
- do while sql_result = 0
-
- /* call sqlexec to fetch a row of data */
- call sqlexec fetch c51 into ':creator, :planname'
-
- /* display any error messages */
- sql_result = check_sql()
-
- /* if successful fetch */
- if sql_result = 0 then do
-
- count = count + 1
-
- /* get rid of trailing blanks */
- planname = strip(planname, T, ' ')
- creator = strip(creator, T, ' ')
-
- /* if first invocation of explain */
- if count = 1 then do
-
- /* this prevents lock contention if the explain plan has
- to be bound */
- call sqlexec commit
-
- /* display any error messages */
- sql_result = check_sql()
-
- end
-
- if sql_result = 0 then do
-
- /* call explain */
- explain dbname planname creator 0 planname'.EXP'
-
- end
- end
- else do
- if sql_result = 100 & count = 0 then do
- parse source with 'COMMAND ' src
- say 'No plans were found in database' dbname
- say 'which match the SQL SELECT statement in' src
- end
- end
- end
-
- /* end-of-file */
- if sql_result = 100 then do
-
- /* call sqlexec to close the cursor */
- call sqlexec close c51
-
- /* display any error messages */
- sql_result = check_sql()
- end
- end
- end
-
- call sqldbs stop using database
- sql_result = check_sql()
-
- /* drop the SQLDBS and SQLEXEC external functions */
- rcy = rxfuncdrop('SQLDBS')
- rcz = rxfuncdrop('SQLEXEC')
- end
- exit sql_result
-
-
-
- check_sql: procedure expose result sqlca. sqlmsg
- if (result <> 0) then do
- sql_result = result
- say 'Result =' result
- end
- else do
- sql_result = sqlca.sqlcode
- if sqlca.sqlcode <> 0 & sqlca.sqlcode <> 100 then
- say sqlmsg
- end
- return sql_result