128

I want to check SID and current database name.

I am using following query for checking oracle SID

select instance from v$thread;

but table or view does not exist error is coming.

I am using following query for checking current database name

select name from v$database;

but table or view does not exist error is coming.

Any idea for above two problems?

1
  • use SELECT INSTANCE_NAME FROM V$INSTANCE;
    – Ahmed Emad
    Dec 23, 2020 at 8:37

8 Answers 8

171

I presume SELECT user FROM dual; should give you the current user

and SELECT sys_context('userenv','instance_name') FROM dual; the name of the instance

I believe you can get SID as SELECT sys_context('USERENV', 'SID') FROM DUAL;

10
  • 1
    Thanks for quick response.Are instance and oracle SID same thing?
    – Adnan
    Jun 9, 2011 at 4:49
  • 2
    @Adnan They need not be the same since there coould be multiple instances of the DB running on a single machine they are identified by SID
    – V4Vendetta
    Jun 9, 2011 at 4:51
  • @adnan Did you get the values you needed ?
    – V4Vendetta
    Jun 9, 2011 at 5:08
  • 13
    select sys_context('userenv','db_name') from dual; for database name and sid i have already added in the answer. hope this gives you what you want
    – V4Vendetta
    Jun 9, 2011 at 5:30
  • 2
    Please check this link
    – V4Vendetta
    Jun 9, 2011 at 6:04
79

If, like me, your goal is get the database host and SID to generate a Oracle JDBC url, as

jdbc:oracle:thin:@<server_host>:1521:<instance_name>

the following commands will help:

Oracle query command to check the SID (or instance name):

select sys_context('userenv','instance_name') from dual; 

Oracle query command to check database name (or server host):

select sys_context('userenv', 'server_host') from dual;

Att. Sergio Marcelo

2
  • Perfect. This is exactly what I wanted to know, but didn't know how to phrase.
    – Addison
    Jul 20, 2016 at 7:13
  • Me too. Thanks Sergio!
    – BenW
    Apr 5, 2017 at 22:31
53

Just for completeness, you can also use ORA_DATABASE_NAME.

It might be worth noting that not all of the methods give you the same output:

SQL> select sys_context('userenv','db_name') from dual;

SYS_CONTEXT('USERENV','DB_NAME')
--------------------------------------------------------------------------------
orcl

SQL> select ora_database_name from dual;

ORA_DATABASE_NAME
--------------------------------------------------------------------------------
ORCL.XYZ.COM

SQL> select * from global_name;

GLOBAL_NAME
--------------------------------------------------------------------------------
ORCL.XYZ.COM
1
  • 1
    No special permissions are needed for the queries above. Verified by creating a new user with CONNECT privilege only.
    – bdeem
    Aug 3, 2015 at 15:18
27

The V$ views are mainly dynamic views of system metrics. They are used for performance tuning, session monitoring, etc. So access is limited to DBA users by default, which is why you're getting ORA-00942.

The easiest way of finding the database name is:

select * from global_name;

This view is granted to PUBLIC, so anybody can query it.

2
  • Whats about oracle SID? Is there any method for checking it from scott account?
    – Adnan
    Jun 9, 2011 at 8:59
  • 1
    That's the service name, not the SID.
    – jpmc26
    Jul 16, 2018 at 23:24
6

Type on sqlplus command prompt

SQL> select * from global_name;

then u will be see result on command prompt

SQL ORCL.REGRESS.RDBMS.DEV.US.ORACLE.COM

Here first one "ORCL" is database name,may be your system "XE" and other what was given on oracle downloading time.

1
3

As has been mentioned above,

select global_name from global_name;

is the way to go.

You couldn't query v$database/v$instance/v$thread because your user does not have the required permissions. You can grant them (via a DBA account) with:

grant select on v$database to <username here>;
3

SID appears to be an overloaded term in the Oracle environment. There's lots of answers on topic which say:
SID = SELECT sys_context('USERENV', 'SID') FROM DUAL;

However please note that this shows your current session_id, and changes for every new connection to the DB.

When referring to SID and current database name in the same question one can safely assume that the OP is trying to configure connections in tnsnames or elsewhere, and not trying to identify the session_id of a currently connected session.

Therefore in this context:
SID = SELECT sys_context('userenv','instance_name') FROM dual;

SERVICE_NAME = select sys_context('userenv','service_name') from dual;

0

SELECT sys_context('userenv','instance_name') FROM dual;

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.