Sql-server – How to choose working database in DB2

db2sql server

In SQL server I can use the USE statement to switch between working databases. Is there a similar command for DB2?

Best Answer

The concept of a "database" in SQL Server is much closer to a schema in DB2. An entire MS SQL database engine is much closer to a DB2 database:

master database <======> SYSIBM/SYSCAT schema
msdb   database <======> SYSPROC       schema

It's not a perfect comparison; there are a number of differences:

  • DB2 does not have an equivalent to the model database.
  • tempdb doesn't really map to a schema; its purpose is handled by system temporary tablespace(s) like TEMPSPACE1.
  • Each database in MS SQL has separate log files, where DB2 uses a single set of transaction logs for the entire database

Therefore, technically the closest equivalent to MS SQL's USE statement is SET SCHEMA. Issuing the SET SCHEMA x statement tells DB2 where to look for unqualified objects.

However, it's not a requirement to issue the SET SCHEMA statement if you fully qualify your object names. The following statements:

set schema x;
select c1, c2 from mytable;

are equivalent to:

select c1, c2 from x.mytable

Using set schema simply allows you to use unqualified objects.