Home » SQL & PL/SQL » SQL & PL/SQL » How to get the session id from pl/sql code?
How to get the session id from pl/sql code? [message #144649] Thu, 27 October 2005 09:58 Go to next message
bes2005
Messages: 33
Registered: September 2005
Location: UK
Member

I need to get the session id from pl/sql code. Can I access V$_SESSION? Any suggestions?

Thanx
Re: How to get the session id from pl/sql code? [message #144654 is a reply to message #144649] Thu, 27 October 2005 10:03 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
SQL> SELECT SYS_CONTEXT('USERENV', 'SESSIONID')
  2    FROM dual
  3  /

SYS_CONTEXT('USERENV','SESSIONID')
-----------------------------------------------
28738608


MHE
Re: How to get the session id from pl/sql code? [message #144661 is a reply to message #144649] Thu, 27 October 2005 10:22 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
One bit remark - USERENV('SESSIONID') and SYS_CONTEXT gives us AUDSID value. For sid and serial# can be so:

SQL> declare
  2   sid_ number;
  3   serial_ number;
  4  begin
  5   select sid, serial# into sid_, serial_
  6   from v$session where sid = (select sid from v$mystat where rownum = 1);
  7   dbms_output.put_line('Session id is ' || sid_);
  8   dbms_output.put_line('Serial# is ' || serial_);
  9  end;
 10  /
Session id is 139
Serial# is 25

PL/SQL procedure successfully completed.

SQL> declare
  2   sid_ number;
  3   serial_ number;
  4  begin
  5   select sid, serial# into sid_, serial_
  6   from v$session where audsid = userenv('sessionid');
  7   dbms_output.put_line('Session id is ' || sid_);
  8   dbms_output.put_line('Serial# is ' || serial_);
  9  end;
 10  /
Session id is 139
Serial# is 25

PL/SQL procedure successfully completed.

Rgds.
Re: How to get the session id from pl/sql code? [message #144680 is a reply to message #144649] Thu, 27 October 2005 12:03 Go to previous message
bes2005
Messages: 33
Registered: September 2005
Location: UK
Member

Thanx for your help.
Previous Topic: prob in package
Next Topic: Substringing the end of a field
Goto Forum:
  


Current Time: Sat Aug 23 10:00:50 CDT 2025