How to view the history of executed SQL statements in DB2?

To view the SQL execution history of DB2, you can use the following method:

  1. To utilize the db2audit tool, DB2 provides a tool called db2audit that can record activity within the database and generate log files. You can enable db2audit using the following command.
  2. Activate the db2audit function.
  3. Once enabled, DB2 will log the history of all SQL executions. You can use the following command to view the log files generated by db2audit:
  4. Extract the db2audit data.
  5. This will extract the log files and display them in the terminal.
  6. DB2 offers a command line tool called db2pd that provides detailed information about the database, including the SQL execution history. You can use the following command to view the SQL execution history.
  7. Retrieve information about the database in reconnection mode using the db2pd command.
  8. This will display detailed information about the database, including the SQL execution history.
  9. The database directory tables in DB2 allow for querying information regarding the history of SQL executions. For example, you can use the following query to view the recently executed SQL statements:
  10. Retrieve the statement text and execution time from the SYSIBMADM.SNAPDB_STATEMENTS table for a specific database partition, sorted in descending order by execution time, and fetching only a specified number of rows.
  11. Replace with the database partition number, and with the number of records you wish to view.

Here are several methods for viewing DB2 SQL historical execution records, you can choose the one that best suits your needs.

bannerAds