简体   繁体   中英

What is the keyword for the database name (not the server) in DB2?

I can get the current database server that I'm logged in to via:

select
  current server
from sysibm.sysdummy1

That's not what I want. My Google searches only gave this solution.

I want the database name.

In TSQL, I could do the following to get the database name:

select db_name()

But, how do I get the current database name in DB2?

EDIT : Thanks for the answers so far. You are correct, I using the wrong term (what would be database in TSQL) for what I am looking to return in DB2. So since I'm not sure what it is called (maybe tablespace):

Is there a command that will return the word "prodbeans" in the following?

select
  <command>
from
  prodbeans.BNSMPLS_PROMO x

As @Leons says in his comment, CURRENT SERVER does return the name of the database.

$ db2 connect to sample

   Database Connection Information

 Database server        = DB2/LINUX 9.1.9
 SQL authorization ID   = IDBJORH
 Local database alias   = SAMPLE

$ db2 values current server

1                 
------------------
SAMPLE            

  1 record(s) selected.

It seems like your confusion arises from the fact that a "database" in SQL server, Sybase and even MySQL are conceptually closer to a either a tablespace or a schema within DB2.

In DB2, a tablespace is a logical container that holds physical database objects (tables, indexes). The tablespace has containers (the physical files that define where the data is written, which are the same thing as a file group in SQL server).

A schema in DB2 is a logical qualifier for objects (tables, indexes, views, etc.). By default, when a user connects to a database, the CURRENT SCHEMA special register defaults to the user's login ID; however this can be changed by using the SET CURRENT SCHEMA statement. CURRENT SCHEMA is used to qualify objects in an SQL statement.

In SQL Server the "master" database is akin to the SYSCATSPACE tablespace within a DB2 database, which stores the system catalog in the SYSCAT (and SYSIBM) schemas. The system catalog tables contain metadata about all objects in the database.

SQL Server's "Tempdb" database equivalent to temporary tablespaces in a DB2 database. By default there is a tablespace called TEMPSPACE1, but there can be multiple temporary tablespaces within a single database.

Msdb corresponds to the SYSTOOLSPACE tablespace (and SYSTOOLS schema).

DB2 does not have an equivalent to the Model database. (DB2 will create an empty database when you execute the "create database" command, but you can't set up a template for what is included in this database.

If your DB2 database is using SQL replication, there may be a set of tables within the database that store replication status and information, but there is no standard naming convention for the schema holding this data. (this would be equivalent to the Distribution database).

There is a "special register" named CURRENT SERVER and one named CURRENT SCHEMA.

See also: how do i get the current schema on DB2 if i have a JDBC conneciton? .

CURRENT SCHEMA is set by default to the username, but can be changed to a more appropriate value. The following will return prodbeans:

SET SCHEMA prodbeans;
select
  CURRENT SCHEMA
from
  BNSMPLS_PROMO x;

Note that you don't need to qualify the table name if you set the schema.

Based on your edited question, I think you are looking for the schema.

For reference purposes, the following will get you a table's tablespace:

SELECT
  tbspace
FROM
  sysibm.systables
WHERE
  name = 'BNSMPLS_PROMO'
  AND creator = 'PRODBEANS';

"name" is the table name and "creator" is the schema name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM