|
| 1 | +REM Filename : proc_sessions.sql |
| 2 | +REM Author : Craig Richards |
| 3 | +REM Created : 15-Feburary-2012 |
| 4 | +REM Version : 1.0 |
| 5 | +REM Modifications : |
| 6 | +REM |
| 7 | +REM Description : Creates two procedures which shows you the last time a password was changed. |
| 8 | +REM The First last_change_all, when called will show you all the users with the last date |
| 9 | +REM The next last_change, when called you pass the user and it displays the details for that user |
| 10 | + |
| 11 | +REM Instructions : For last_change call it by exec last_change('username') |
| 12 | + |
| 13 | +CREATE OR REPLACE PROCEDURE sessions (inp_user VARCHAR2) |
| 14 | +AUTHID CURRENT_USER |
| 15 | +AS |
| 16 | + |
| 17 | +-- Variable Declaration |
| 18 | + |
| 19 | + lv_username sys.v$session.username%TYPE; |
| 20 | + lv_sid sys.v$session.sid%TYPE; |
| 21 | + lv_serial# sys.v$session.serial#%TYPE; |
| 22 | + lv_status sys.v$session.status%TYPE; |
| 23 | + lv_active_count NUMBER; |
| 24 | + lv_inactive_count NUMBER; |
| 25 | + |
| 26 | +-- Create the cursors |
| 27 | + |
| 28 | + CURSOR c_active IS |
| 29 | + SELECT username, sid, serial#, status |
| 30 | + FROM sys.V$session |
| 31 | + WHERE UPPER(USERNAME) = UPPER(inp_user) |
| 32 | + AND status = 'ACTIVE'; |
| 33 | + |
| 34 | + CURSOR c_inactive IS |
| 35 | + SELECT RPAD(username,24,' '), sid, serial#, status |
| 36 | + FROM sys.V$session |
| 37 | + WHERE UPPER(USERNAME) = UPPER(inp_user) |
| 38 | + AND status != 'ACTIVE'; |
| 39 | + |
| 40 | + CURSOR c_active_count IS |
| 41 | + select count(*) from v$session where status = 'ACTIVE' |
| 42 | + and UPPER(USERNAME) = UPPER(inp_user); |
| 43 | + |
| 44 | + CURSOR c_inactive_count IS |
| 45 | + select count(*) from v$session where status != 'ACTIVE' |
| 46 | + and UPPER(USERNAME) = UPPER(inp_user); |
| 47 | + |
| 48 | +-- Output the Information |
| 49 | + |
| 50 | + BEGIN |
| 51 | + DBMS_OUTPUT.PUT_LINE('Session Counts :'); |
| 52 | + DBMS_OUTPUT.PUT_LINE('================'); |
| 53 | + OPEN c_active_count; |
| 54 | + LOOP |
| 55 | + FETCH c_active_count INTO lv_active_count; |
| 56 | + EXIT WHEN c_active_count%NOTFOUND; |
| 57 | + DBMS_OUTPUT.PUT_LINE(inp_user || ' has ' || lv_active_count || ' active sessions'); |
| 58 | + END LOOP; |
| 59 | + CLOSE c_active_count; |
| 60 | + |
| 61 | + OPEN c_inactive_count; |
| 62 | + LOOP |
| 63 | + FETCH c_inactive_count INTO lv_inactive_count; |
| 64 | + EXIT WHEN c_inactive_count%NOTFOUND; |
| 65 | + DBMS_OUTPUT.PUT_LINE(inp_user || ' has ' || lv_inactive_count || ' inactive sessions'); |
| 66 | + END LOOP; |
| 67 | + CLOSE c_inactive_count; |
| 68 | + |
| 69 | + OPEN c_active; |
| 70 | + DBMS_OUTPUT.PUT_LINE(CHR(10)); |
| 71 | + DBMS_OUTPUT.PUT_LINE('Active Session Information for : ' || inp_user); |
| 72 | + DBMS_OUTPUT.PUT_LINE(CHR(10)); |
| 73 | + DBMS_OUTPUT.PUT_LINE('USERNAME' || CHR(9) || 'SID' || CHR(9) || 'SERIAL#' || CHR(9) || 'STATUS'); |
| 74 | + LOOP |
| 75 | + FETCH c_active INTO lv_username , lv_sid, lv_serial#, lv_status; |
| 76 | + EXIT WHEN c_active%NOTFOUND; |
| 77 | + DBMS_OUTPUT.PUT_LINE(lv_username || CHR(9) || CHR(9) || lv_sid || CHR(9) || lv_serial# || CHR(9) || lv_status); |
| 78 | + END LOOP; |
| 79 | + CLOSE c_active; |
| 80 | + |
| 81 | + OPEN c_inactive; |
| 82 | + DBMS_OUTPUT.PUT_LINE(CHR(10)); |
| 83 | + DBMS_OUTPUT.PUT_LINE('Inactive Session Information for : ' || inp_user); |
| 84 | + DBMS_OUTPUT.PUT_LINE(CHR(10)); |
| 85 | + DBMS_OUTPUT.PUT_LINE('USERNAME' || CHR(9) || 'SID' || CHR(9) || 'SERIAL#' || CHR(9) || 'STATUS'); |
| 86 | + LOOP |
| 87 | + FETCH c_inactive INTO lv_username , lv_sid, lv_serial#, lv_status; |
| 88 | + EXIT WHEN c_inactive%NOTFOUND; |
| 89 | + DBMS_OUTPUT.PUT_LINE(lv_username || CHR(9) || CHR(9) || lv_sid || CHR(9) || lv_serial# || CHR(9) || lv_status); |
| 90 | + END LOOP; |
| 91 | + CLOSE c_inactive; |
| 92 | +END sessions; |
| 93 | +/ |
| 94 | +SHOW ERROR |
0 commit comments