Skip to Main Content
  • Questions
  • Finding host name or connect string after database login.

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, malcolm.

Asked: June 07, 2000 - 1:41 pm UTC

Last updated: April 06, 2004 - 9:21 am UTC

Version: Oracle 7 and/or 8

Viewed 10K+ times! This question is

You Asked

From SQL*Plus, how can I determine the host to which I am connected?

I commonly work on a set of distributed systems, which, combined with different server versions and mainenance accounts, adds up to about 200 different systems I might be working on, and many of them look very similar.

I often wish I could reconfirm which system I am working on.

It is super easy to recheck the userid and the server version, which helps, but I have yet to find a way (short of explicitly re-connecting) to confirm what host system I am working on.

A way to find the original connect string would be best, but any host identifying detail would be useful.

Many systems are running 7.3.4.1.0. They are being switched, slowly, to version 8. Currently that seems to mean 8.1.6.0.0. (I can use SQL*Plus 3.3.2.0.2, or 8.0.5.0.0, or could install another I'm sure, if that makes any difference.)

Thanks for any infomation you could provide.


and Tom said...

I'm in the same boat (lots of databases, wanna know which one I'm in).

I use a login.sql with SQLPlus. I also make sure each and every database has a unique global name (alter database rename global_name to <some name>) or unique SID (i like the global name myself).

In my login.sql i have:

....
column global_name new_value gname
set termout off
select lower(user) || '@' ||
decode(global_name, 'ORACLE8.WORLD', '8.0', 'ORA8I.WORLD',
'8i', 'ORA8IDEV.WORLD', 'dev8i', global_name ) global_name from global_name;
set termout on
set sqlprompt '&gname> '
.....

That makes my sqlprompt look like:

ops$tkyte@DEV8I.WORLD>

So, now I have my username and database in the prompt. I just have to remember to use an @connect -- NOT connect when connecting since the login.sql is run ONCE, not after every connect. My @connect is simply:

...
connect &1
@login
...


If you want to use the ORACLE_SID instead of the global database name, just "select instance global_name from v$thread" in the above.




Rating

  (11 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Now I always know where I am.

Johann Lodina, May 30, 2001 - 1:41 pm UTC

Thank you for this hint, it is exactly what I looked for.

A reader, September 13, 2001 - 4:11 am UTC


It was a solution I really needed!! Thanks!!

Shashank Sinha, October 17, 2001 - 10:00 am UTC

This site itself is pretty great, and the search engine is neat!!
This solution in particular was something that will save a lot of my hair...!!

Instance and hostname on SQL*Plus login

Angus McTavish aged 75, September 26, 2003 - 7:37 am UTC

Sometimes you might want more info than you can get from global_name, for example the instance and the hostname for the database. Problem with that is that the user may not have access to the v$ tables. I use a modified version of your script which first gets the info from global_name and then tries to get info from v$instance for example. If the user doesn't have access to v$instance at least the global_name info will be displayed. If you simply use your script (but replace global_name with v$instance), when you switch from a database where you do have access to v$instance to one where you don't, the login will work but you will display the login details from the first database. Know what I mean?

Here is an example of the modified script anyway:

column global_name new_value gname
set termout off
select lower(user) || '@' ||
decode(global_name, 'ORACLE8.WORLD', '8.0', 'ORA8I.WORLD',
'8i', 'ORA8IDEV.WORLD', 'dev8i', global_name ) global_name from global_name;
select lower(user) || ':'|| instance_name ||'@'|| host_name global_name from v$instance;
set termout on
set sqlprompt '&gname> '

angus:TRFG@angustest> @connect angus/x@pan1
Connected.
angus:pan1@PANSYS01> @connect noddy/noddy@pan1
Connected.
noddy@PAN1.US.ORACLE.COM>

Placing this SQL file

A reader, March 17, 2004 - 4:09 pm UTC

Hi,

I've placed the connect.sql in the same directory where I have glogin.sql (using Oracle 9i). But for some reason, when I try to execute @connect, I get the error message - connect.sql is not found.

How can I set the default directory to be ora9i\sqlplus\admin.

Please suggest

Tom Kyte
March 17, 2004 - 4:22 pm UTC

set your SQLPATH environment variable. that is where sqlplus looks for scripts.

sqlplus /nolog

Arun Gupta, March 17, 2004 - 5:26 pm UTC

Tom,
If I do a sqlplus /nolog, login.sql asks for a value for gname since I am not yet connected to any database. Is there a workaround?
Thanks


Tom Kyte
March 17, 2004 - 6:30 pm UTC


define gname=idle
column global_name new_value gname
select lower(user) || '@' || substr( global_name, 1, decode( dot, 0, length(global_name), dot-1) ) global_name
from (select global_name, instr(global_name,'.') dot from global_name );
set sqlprompt '&gname> '


will default the prompt to "idle" in the event the query cannot run

Oracle Mode Details

Dhimant Antani, March 18, 2004 - 12:14 am UTC

Additionally how can I know which mode the server is running in i.e Dedicated or Shared ??? Any help will be most appreciated


Regards,

Dhimant Antani

Tom Kyte
March 18, 2004 - 7:21 am UTC

the "server" does not "run in dedicated or shared"

the "server" can accept both connection types.


select server, count(*) from v$session;

will show you at a point in time how many of each kind you have.

dedicated = dedicated server
shared,none = shared server

connect as sysdba

Arun Gupta, March 18, 2004 - 9:36 am UTC

One more problem with connect as sysdba:

idle> @connect sys/xyz@trng as sysdba
ERROR:
ORA-28009: connection to sys should be as sysdba or sysoper

Thanks...

Tom Kyte
March 18, 2004 - 9:52 am UTC

@connect "sys/.... as sysdba"

my script only uses &1

10g sqlplus

Dave, March 18, 2004 - 1:16 pm UTC

10g sqlplus will re-read the login file, so no need for @connect now hopefully

Tom Kyte
March 18, 2004 - 3:16 pm UTC

correct

ops$tkyte@ORA10G> connect scott/tiger
Connected.
scott@ORA10G>



 

ora_database_name

Muraalee, April 06, 2004 - 3:21 am UTC

Tom
I wonder if I could use 'ora_database_name' in place of 'global_name'

rmurali@ORA9R2> select global_name from global_name;

GLOBAL_NAME
------------------------------------------------------
ORA9R2.WORLD

Elapsed: 00:00:00.00
rmurali@ORA9R2> select ora_database_name from dual;

ORA_DATABASE_NAME
------------------------------------------------------
ORA9R2.WORLD

Elapsed: 00:00:00.00
rmurali@ORA9R2>

Actually, my question is -
what is the difference between using ora_database_name and global_name

Thanks.

Tom Kyte
April 06, 2004 - 9:21 am UTC

ora_database_name is a function that returns the global name.

 
ops$tkyte@ORA9IR2> exec dbms_output.put_line( ora_database_name );
ORA9IR2.US.ORACLE.COM
 
PL/SQL procedure successfully completed.


global_name is a column in a table.

ops$tkyte@ORA9IR2> desc global_name;
 Name                                 Null?    Type
 ------------------------------------ -------- -------------------------
 GLOBAL_NAME                                   VARCHAR2(4000)
 
ops$tkyte@ORA9IR2> select * from global_name;
 
GLOBAL_NAME
------------------------------------------------------------------------
ORA9IR2.US.ORACLE.COM


yes, you can. 

Thanks

Suraj Sharma, March 29, 2007 - 2:06 am UTC

Thanks a lot Tom for shareing such a usuful information with us.