How to get the last executed SQL statement and bind variable values in oracle

后端 未结 4 1925
感动是毒
感动是毒 2021-02-02 15:21

I have written the following query to get the last executed SQL statement in the oracle database for a particular session. The SQL text does not contain the actual value of the

4条回答
  •  滥情空心
    2021-02-02 15:52

    Run the below query which takes the sql_id as the input parameter and will give the output with replaced bind variable values.

    set serveroutput on;
    
    
    DECLARE
       v_fulltext   CLOB;
       v_sql_id     VARCHAR2 (100);
    
       CURSOR c1( v_sql_id varchar2)
       IS
          SELECT decode(substr(NAME,1,4),':SYS',replace(name,':',':"')||'"' ,NAME ) NAME, POSITION, datatype_string,nvl(VALUE_STRING,'NULL') value_string
        FROM v$sql_bind_capture
           WHERE sql_id = v_sql_id;
    BEGIN
    
      v_sql_id:= '&sql_id';
    
       SELECT sql_fulltext
         INTO v_fulltext
         FROM v$sql
        WHERE sql_id =v_sql_id  AND ROWNUM = 1;
    
       FOR rec IN c1(v_sql_id)
       LOOP
    
    
          IF substr(rec.datatype_string,1,8) = 'VARCHAR2'
          THEN
         SELECT REPLACE (v_fulltext,
                 rec.NAME,
                 '''' || rec.value_string || ''''
                )
           INTO v_fulltext
           FROM DUAL;
          END IF;
    
          IF rec.datatype_string = 'NUMBER'
          THEN
         SELECT REPLACE (v_fulltext, rec.NAME, rec.value_string)
           INTO v_fulltext
           FROM DUAL;
          END IF;
    
       END LOOP;
       DBMS_OUTPUT.PUT_LINE(v_fulltext);
    
       EXCEPTION
       WHEN NO_DATA_FOUND
       THEN DBMS_OUTPUT.PUT_LINE('NO SQL FOUND FOR THE SQL ID');
    
    END;
    /
    

提交回复
热议问题