抓取锁的sql语句-第四次修改

--完成情况   变量V_BLOCKING_SID 用来动态抓取 产生锁的会话id,输出参数没有任何问题,但是执行报错  标识符无效!

CREATE OR REPLACE PROCEDURE SOLVE_LOCK123

AS

V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句
V_SQL02 VARCHAR2(3000);
V_SQL03 VARCHAR2(3000);

KILL_SID NUMBER;
KILL_SERIAL NUMBER;

CUR_LOCK SYS_REFCURSOR; --定义游标变量,循环执行抓取锁的sql语句
CUR_LOCK02 SYS_REFCURSOR;

TYPE TP_LOCK IS RECORD( --定义 record类型的 变量
V_SID NUMBER,
V_TYPE VARCHAR2(10),
V_ID1 NUMBER,
V_ID2 NUMBER,
V_LMODE VARCHAR2(200),
V_REQUEST VARCHAR2(200),
V_LOCK_TIME NUMBER,
V_BLOCK NUMBER );
RECORDS_LOCK TP_LOCK;

TYPE TP_LOCK02 IS RECORD(
WAITING_SID NUMBER,
WAITING_SQL VARCHAR2(1000),
BLOCKER_EVENT VARCHAR2(1000),
BLOCKING_SID NUMBER,
BLOCKING_SQL VARCHAR2(1000));
RECORDS_LOCK02 TP_LOCK02;

V_BLOCKING_SID NUMBER;
V_WAITING_SID NUMBER;

BEGIN
V_SQL:=‘SELECT SID,TYPE,ID1,ID2,
DECODE(LMODE,0, ‘‘NONE‘‘,1,‘‘NULL‘‘, 2, ‘‘ROW SHARE‘‘ ,3, ‘‘ROW EXCLUSIVE‘‘ ,4, ‘‘SHARE‘‘ ,5, ‘‘SHARE ROW EXCLUSIVE‘‘ ,6 ,‘‘EXCLUSIVE‘‘ ,‘‘HAHA‘‘) LOCK_TYPE,
DECODE(REQUEST,0, ‘‘NONE‘‘,1,‘‘NULL‘‘, 2, ‘‘ROW SHARE‘‘ ,3, ‘‘ROW EXCLUSIVE‘‘ ,4, ‘‘SHARE‘‘ ,5, ‘‘SHARE ROW EXCLUSIVE‘‘ ,6 ,‘‘EXCLUSIVE‘‘ ,‘‘HAHA‘‘) LOCK_REQUEST,
CTIME,BLOCK FROM V$LOCK WHERE TYPE IN (‘‘TM‘‘,‘‘TX‘‘)‘ ;

OPEN CUR_LOCK FOR V_SQL;
LOOP
FETCH CUR_LOCK INTO RECORDS_LOCK;
EXIT WHEN CUR_LOCK%NOTFOUND;

IF RECORDS_LOCK.V_REQUEST <> ‘NONE‘ THEN --抓取发出请求锁的会话
DBMS_OUTPUT.PUT_LINE(‘waiting sid: ‘||RECORDS_LOCK.V_SID||‘ is request a lock ,lock_mode is ‘||RECORDS_LOCK.V_REQUEST||‘ and being locked ‘|| RECORDS_LOCK.V_LOCK_TIME||‘s‘);
V_WAITING_SID:=RECORDS_LOCK.V_SID;
END IF;

IF RECORDS_LOCK.V_BLOCK <> 0 THEN --抓取发生锁阻塞的会话
DBMS_OUTPUT.put_line(‘blocking sid: ‘||RECORDS_LOCK.V_SID||‘ is make a lock , lock_mode is ‘||RECORDS_LOCK.V_LMODE);
V_BLOCKING_SID:=RECORDS_LOCK.V_SID;
DBMS_OUTPUT.put_line(V_BLOCKING_SID);
END IF;
END LOOP;
CLOSE CUR_LOCK;

V_SQL02:=‘SELECT DISTINCT
WAITER.SID "WAITING_SID",
W_SQL.SQL_TEXT "SQL FROM WAITING SESSION",

BLOCKER.EVENT "BLOCKER EVENT",
BLOCKER.SID "BLOCKING SID",

B_SQL.SQL_TEXT "SQL FROM BLOCKING SESSION"

FROM V$SESSION WAITER,
V$SESSION BLOCKER,
V$SQL W_SQL,
V$SQL B_SQL
WHERE WAITER.EVENT=‘‘enq: TX - row lock contention‘‘ AND WAITER.BLOCKING_SESSION=BLOCKER.SID
AND W_SQL.SQL_ID=WAITER.SQL_ID
AND B_SQL.SQL_ID =NVL(BLOCKER.SQL_ID,BLOCKER.PREV_SQL_ID)‘;

OPEN CUR_LOCK02 FOR V_SQL02;
LOOP  
FETCH CUR_LOCK02 INTO RECORDS_LOCK02;
EXIT WHEN CUR_LOCK02%NOTFOUND;

IF RECORDS_LOCK02.WAITING_SID IS NOT NULL THEN
DBMS_OUTPUT.put_line(‘waiting sid: ‘||RECORDS_LOCK02.WAITING_SID||‘ WAIT_SQL IS: ‘||RECORDS_LOCK02.WAITING_SQL);
END IF;

IF RECORDS_LOCK02.BLOCKING_SID IS NOT NULL THEN
DBMS_OUTPUT.put_line(‘blocking sid: ‘||RECORDS_LOCK02.BLOCKING_SID||‘ BLOCK_SQL IS: ‘||RECORDS_LOCK02.BLOCKING_SQL);
END IF;

END LOOP;

CLOSE CUR_LOCK02;

DBMS_OUTPUT.put_line(V_BLOCKING_SID);
-- 解决 锁阻塞、锁等待
V_SQL03:=‘SELECT  SID , SERIAL#  FROM V$SESSION WHERE SID=V_BLOCKING_SID‘ ;

EXECUTE IMMEDIATE V_SQL03 INTO KILL_SID,KILL_SERIAL;
--‘SELECT  SID ,SERIAL# FROM V$SESSION WHERE SID=197 ‘ into kill_sid,KILL_SERIAL;

DBMS_OUTPUT.put_line(‘ALTER SYSTEM KILL SESSION (‘||KILL_SID||‘,‘||KILL_SERIAL||‘)‘);

END SOLVE_LOCK123;

时间: 2024-10-15 20:51:02

抓取锁的sql语句-第四次修改的相关文章

抓取锁的sql语句-第六次修改

增加异常处理 CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 VARCHAR2(3000); KILL_SID NUMBER;KILL_SERIAL NUMBER; CUR_LOCK SYS_REFCURSOR; --定义游标变量,循环执行抓取锁的sql语句CUR_LOCK02 SYS_REFCURSOR; TYPE TP_LOCK IS RECORD( -

抓取锁的sql语句-第五次修改

CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 VARCHAR2(3000);V_SQL03 VARCHAR2(3000); KILL_SID NUMBER;KILL_SERIAL NUMBER; CUR_LOCK SYS_REFCURSOR; --定义游标变量,循环执行抓取锁的sql语句CUR_LOCK02 SYS_REFCURSOR; TYPE TP_L

抓取锁的sql语句-第三次修改

CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句CUR_LOCK SYS_REFCURSOR; --定义游标变量,循环执行抓取锁的sql语句 TYPE TP_LOCK IS RECORD( --定义 record类型的 变量V_SID NUMBER,V_TYPE VARCHAR2(10),V_ID1 NUMBER,V_ID2 NUMBER,V_LMODE VARCHAR2(

抓取锁的sql语句-第二次修改

CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000);  --定义 v_sql 接受抓取锁的sql语句 CUR_LOCK SYS_REFCURSOR; --定义游标变量,循环执行抓取锁的sql语句 TYPE TP_LOCK IS RECORD(   --定义 record类型的 变量  V_SID NUMBER, V_TYPE VARCHAR2(10), V_ID1 NUMBER, V_ID2 NUMBER, V_LMODE

SQL语句汇总(二)——数据修改、数据查询

首先创建一张表如下,创建表的方法在上篇介绍过了,这里就不再赘述. 添加新数据: INSERT INTO <表名> (<列名列表>) VALUES (<值列表>)  如: INSERT INTO t_student (student_id,student_name,student_age,student_sex) VALUES (1,'大毛',18,'男'); 其中列名可以省略,省略之后要求插入的值必须与列一一对应: INSERT INTO t_student VALUE

SQL语句增加列、修改列、删除列

SQL语句增加列.修改列.删除列 1.增加列: 1.alter table tableName add columnName varchar(30) 2.ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ; 2.1. 修改列类型: 1.alter table tableName alter column columnName varchar(4000) 2.2. 修改列的名称: 1.EXEC sp_r

sql server抓取表结构的语句

sql server 2008抓取方法: --------------------------------------- SELECT 表名 = Case When A.colorder=1 Then D.name Else '' End, 表说明 = Case When A.colorder=1 Then isnull(F.value,'') Else '' End, 字段序号 = A.colorder, 字段名 = A.name, 字段说明 = isnull(G.[value],''), 标

SQL语句增加字段、修改字段、修改类型、修改默认值

--一.修改字段默认值 alter table 表名 drop constraint 约束名字 ------说明:删除表的字段的原有约束 alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称 -------说明:添加一个表的字段的约束并指定默认值 --二.修改字段名: alter table 表名 rename column A to B --三.修改字段类型: alter table 表名 alter column UnitPrice

Oracle SQL语句大全(四)

PL/SQL里面执行DDL语句  begin  execute immediate 'create table T(nnn varchar2(20) default ''aaa'')';  end;  PL/SQL的分支语句: declare     v_sal emp.sal%type; begin     select sal into v_sal from emp     where empno=7369;    if(v_sal<1200) then       dbms_output.