Skip to Main Content
  • Questions
  • How to get database name in oracle db node js

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, gopi.

Asked: July 06, 2016 - 11:24 am UTC

Last updated: July 12, 2016 - 10:35 am UTC

Version: node-oracledb version 1.9

Viewed 10K+ times! This question is

You Asked


How to get database name in oracle db in node js without query using connection object
var oracledb = require('oracledb');
oracledb.getConnection(
    {
        user: 'c##root',
        password: 'root',
        connectString: 'localhost:1521/orcl'
    },
    function (err, connection) {

        if (err) {
            console.log('error while connect to OracleDB :' + err.stack);
            return;
        } else {
            console.log("Database connected")
            connection.execute("INSERT INTO person (name) VALUES  ( :NAME) ", ['DOE'], function (err, result) {
                if (err) {
                    console.error(err);
                    return;
                } else {
                    console.log("Value sucessfully added to database: " + "'database name here'")
                }
            });
        }
    });


------------------------------------
I want to get name of the database when each time execute particular query.

and Connor said...

sys.database_name returns the database name, and sys_context('USERENV','INSTANCE_NAME') will return the name of the instance (which might NOT be the same as the name of the database).

eg


SQL> select sys.database_name from dual;

DATABASE_NAME
-----------------------------------------------
NP12


So you can run the query above, or add the function to any existing query you have

Rating

  (3 ratings)

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

Comments

Is any other way to get database name other than executing query

Gopi, July 07, 2016 - 5:42 pm UTC

In my case i creating API for query execution. I want to show database name for corresponding query when each time query execute. I don't wan't to execute one more query for database name. And also i don't want to change the original query to get database name. My question is there is any other way to get database name like using connection object or using result after query execution.

var oracledb = require('oracledb');
oracledb.getConnection(
    {
        user: 'c##root',
        password: 'root',
        connectString: 'localhost:1521/orcl'
    },
    function (err, connection) {

        if (err) {
            console.log('error while connect to OracleDB :' + err.stack);
            return;
        } else {
            console.log("Database connected")
            connection.execute("INSERT INTO person (name) VALUES  ( :NAME) ", ['DOE'], 
function (err, result) {
                if (err) {
                    console.error(err);
                    return;
                } else {
                    <b>console.log("Value sucessfully added to database: " + "'database name 
here'")</b>
                }
            });
        }
    });


In above example i want to show database name in console (where highlighted bold) without one more query and without change the above query.

Thanks
Chris Saxon
July 09, 2016 - 1:48 am UTC

I got this from Dan McGhan who works on the driver.

"This can be done easily today by adding properties to the connection when created or retrieved from a pool"

If that is not enough info, let me know and I'll ask for a clarification.

Cheers,
Connor

Without query execution like select sys.database_name from dual, how to get database name .

Gopi, July 09, 2016 - 8:44 am UTC

Is there any way to get database name other than executing query like select sys.database_name from dual.
Can i get database name using connection object .
connection.getDatabaseName()
(Or)
Using result after executing any database query
result.getDatabaseName()

If any one know solution please describe with example for better understanding.
Thanks.

problem solved.

gopi, July 12, 2016 - 10:14 am UTC

problem solved by adding property to the connection object.

Thanks
Connor McDonald
July 12, 2016 - 10:35 am UTC

Glad you solved it.