SQLDropDb function (CLI) - Drop a database

The SQLDropDb() function drops the specified database.

Specification:

  • CLI V9.7
Unicode Equivalent: The corresponding Unicode function is the SQLDropDbW() function. For information about ANSI to Unicode function mappings, see Unicode functions (CLI).

Syntax

SQLRETURN SQL_API_FN SQLDropDb ( SQLHDBC      hDbc,
                                 SQLCHAR      *szDbName,
                                 SQLINTEGER   cbDbName); 

Function arguments

Table 1. SQLDropDb function argument
Data type Argument Use Description
SQLHDBC hDbc input Connection handle.
SQLCHAR * szDbName input Name of the database that is to be dropped.
SQLINTEGER cbDbName input Number of SQLCHAR elements (or SQLWCHAR elements for the Unicode variant of the function) that is needed to store the szDbName argument or to store SQL_NTS if the szDbName is null terminated.

Usage

To drop a Db2® database, the CLI application must first attach to the server instance by using the ATTACH keyword. The valid APIs, after connecting to the server instance using ATTACH keyword are SQLCreateDb(), SQLDropDb(), and SQLDisconnect().

There are multiple ways to attach to an instance:
  • For a local attachment, use the connection string attach=true.
  • For a local attachment over TCP/IP, configure the instance to allow TCP/IP connections. For usage of the TCP/IP connection, use the connection string:
    attach=true;HOSTNAME=localhost;PORT=portno;UID=username;PWD=password;
  • For a remote attachment, configure the remote instance to allow TCP/IP connections. For remote attachment, use the connection string:
    attach=true;HOSTNAME=localhost;PORT=portno;UID=username;PWD=password;

Return codes

  • SQL_SUCCESS
  • SQL_ERROR

Restrictions

  • An already connected database cannot be dropped.
  • The SQLDropDb() function is not supported for Db2 for IBM® i and Db2 for z/OS® data servers.

Example

The following example shows the command syntax for creating and dropping Db2 databases on a local server. The instance is configured to allow TCP connections:
SQLHANDLE hdbc;

// Both connection strings attach to a local instance but take different routes.
SQLCHAR connString[1024] = "attach=true;";
// If the preference is to use the TCP/IP protocol (and instance was configured to allow TCP/IP connections) to attach to the local instance use:
//SQLCHAR connString[1024] = "attach=true;HOSTNAME=localhost;PORT=portno;UID=username;PWD=password; ";

SQLRETURN rc = 0;
rc = SQLDriverConnect((SQLHDBC)hdbc, (SQLHWND)NULL, connString, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
if ( rc != SQL_SUCCESS ) 
{ 
  //check the error code; 
  return -1; 
}
rc = SQLCreateDB(hdbc, sample1, 8, NULL, 0, NULL, 0) 
rc = SQLCreateDB(hdbc, sample2, 8, NULL, 0, NULL, 0)
rc = SQLDropDb(hdbc, sample1, 8);
rc = SQLDropDb(hdbc, sample2, 8);
rc = SQLDisconnect(hdbc);
The following example shows the command syntax for creating and dropping Db2 databases on a remote server:
SQLHANDLE hdbc;
SQLCHAR connString[1024] = "attach=true;HOSTNAME=remotehost;PORT=portno;UID=username;PWD=password; ";
SQLRETURN rc = 0;
rc = SQLDriverConnect((SQLHDBC)hdbc, (SQLHWND)NULL, connString, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
if ( rc != SQL_SUCCESS ) 
{ 
   //check the error code; 
   return -1; 
} 
rc = SQLCreateDB(hdbc, sample1, 8, NULL, 0, NULL, 0) 
rc = SQLCreateDB(hdbc, sample2, 8, NULL, 0, NULL, 0)
rc = SQLDropDb(hdbc, sample1, 8);
rc = SQLDropDb(hdbc, sample2, 8);
rc = SQLDisconnect(hdbc);

Version information

Last update
This topic was last updated for IBM Db2 Version 9.7, Fix Pack 3.
IBM Data Server Client
Supported in IBM Db2