oracle 有什么办法可以从oracle中的gv$session获取当前会话的信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28814543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:45:40  来源:igfitidea点击:

Is there any way to get information about current session from gv$session in oracle?

oraclesessionoracle-rac

提问by Volodymyr Frolov

Is there any way to uniquely identify current session in GV$SESSIONview in Oracle?

有没有办法GV$SESSION在Oracle 中唯一标识当前会话?

I've faced with the problem that the following query may return more than one row in case of Oracle RAC configuration:

我遇到的问题是,在 Oracle RAC 配置的情况下,以下查询可能返回多于一行:

SELECT SID, SERIAL#
FROM GV$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID')
   AND SID = Sys_Context('USERENV', 'SID');

Using V$MYSTATis not an option either, because V$MYSTATmay not be accessible for the current session (for example when statistic is disabled).

使用V$MYSTAT也不是一个选项,因为V$MYSTAT当前会话可能无法访问(例如,当统计信息被禁用时)。

回答by Mark J. Bobak

Try this:

尝试这个:

SELECT SID, SERIAL#
FROM V$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID');

Since you're interested in current session, the current session must be on the local instance (by definition), so use V$SESSIONinstead of GV$SESSION. Also, all you need is AUDSIDto uniquely identify your session.

由于您希望在本届会议上,本届会议必须在本地实例(定义),所以使用V$SESSION的替代GV$SESSION。此外,您所需要的只是AUDSID唯一标识您的会话。

If you've got some reason you reallyneed to use GV$SESSION(can't imagine why that would be), you could do this instead:

如果您有某些原因确实需要使用GV$SESSION(无法想象为什么会这样),您可以这样做:

SELECT SID, SERIAL#
    FROM GV$SESSION
    WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID')
      AND INST_ID = USERENV('Instance');

Also, an alternate way to get the SIDof the current session is:

此外,获取SID当前会话的另一种方法是:

select sid from v$mystat where rownum=1;

Hope that helps.

希望有帮助。

回答by aitor

Joining gv$sessionand v$mystat:)

加入gv$sessionv$mystat:)

 SELECT SID, SERIAL#,inst_id
  FROM GV$SESSION
 WHERE sid=(select sid from v$mystat where rownum=1); 

回答by GolezTrol

Use V$SESSIONinstead of GV$SESSION

使用V$SESSION代替GV$SESSION

SELECT SID, SERIAL#
FROM V$SESSION
WHERE SID = Sys_Context('USERENV', 'SID');

回答by Vikas

Try this (will need access to v$session and v$sql views) -

试试这个(需要访问 v$session 和 v$sql 视图)-

select /*My Sess*/ 'My Sess Text = hello world @ '||systimestamp SID_SR_TXT from dual
union
select 'SID = '||sid||' Serial# = '||serial# SID_SR_TXT  from v$session where sql_id in 
(select sql_id from v$sql where sql_text like '% My Sess %');