SQL by Session ID (sqlbysid.sql)
What This Script Does
Section titled “What This Script Does”This script retrieves the complete SQL text currently being executed by a specific session ID. Essential for diagnosing performance issues and understanding what SQL statements are running in problematic sessions.
The Script
Section titled “The Script”SET LINESIZE 500SET PAGESIZE 1000SET VERIFY OFF
SELECT a.sql_textFROM v$sqltext a, v$session bWHERE a.address = b.sql_addressAND a.hash_value = b.sql_hash_valueAND b.sid = &sidORDER BY a.piece;
PROMPTSET PAGESIZE 14
Parameters
Section titled “Parameters”The script prompts for:
- sid: Session ID to analyze
-- Basic usage (will prompt for SID)@sqlbysid.sql
-- Example when prompted:-- Enter value for sid: 123
Sample Output
Section titled “Sample Output”SQL_TEXT--------------------------------------------------------------------------------SELECT /*+ FIRST_ROWS(100) */ c.customer_id, c.customer_name, o.order_date, o.total_amountFROM customers cJOIN orders o ON c.customer_id = o.customer_idWHERE c.region = 'NORTH' AND o.order_date >= SYSDATE - 30ORDER BY o.order_date DESC