溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

安全地刪除數(shù)據(jù)庫(kù)用戶

發(fā)布時(shí)間:2020-07-09 20:56:21 來(lái)源:網(wǎng)絡(luò) 閱讀:784 作者:紫夜寒冰 欄目:關(guān)系型數(shù)據(jù)庫(kù)

SET SERVEROUT ON

CREATE OR REPLACE PROCEDURE gracefullyDropUser(v_username IN VARCHAR2) IS
l_cnt integer;
sqlStmt VARCHAR2(1000);
BEGIN
sqlStmt := 'alter user ' || v_username || ' account lock'; --鎖住用戶,防止其他新會(huì)話的連接
EXECUTE IMMEDIATE sqlStmt;
dbms_output.put_line(sqlStmt);
FOR x IN (SELECT * FROM v$session WHERE username = v_username) LOOP
sqlStmt := 'alter system disconnect session ''' || x.sid || ',' ||
x.serial# || ''' IMMEDIATE'; --循環(huán)disconnect每一個(gè)已存的session連接,并等待所有session被刪除
EXECUTE IMMEDIATE sqlStmt;
dbms_output.put_line(sqlStmt);
END LOOP;

-- Wait until all sessions are disconnected forcely, check every 2 seconds
LOOP
SELECT COUNT(*) INTO l_cnt FROM v$session WHERE username = v_username;
EXIT WHEN l_cnt = 0;
dbms_lock.sleep(2);
dbms_output.put_line('hold on ...');
END LOOP;
sqlStmt := 'drop user ' || v_username || ' cascade'; --安全地刪除數(shù)據(jù)庫(kù)用戶
EXECUTE IMMEDIATE sqlStmt;
dbms_output.put_line(sqlStmt);
END gracefullyDropUser;
/

execute gracefullyDropUser('AGILE');
alter user AGILE account lock
alter system disconnect session '12,97' IMMEDIATE
alter system disconnect session '20,11' IMMEDIATE
alter system disconnect session '141,118' IMMEDIATE
hold on ...
hold on ...
drop user AGILE cascade

PL/SQL procedure successfully completed.

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI