SQL Reference
The CONNECT (Type 1) statement connects an application process to the
identified application server according to the rules for remote unit of
work.
An application process can only be connected to one application server at a
time. This is called the current server. A default
application server may be established when the application requester is
initialized. If implicit connect is available and an application
process is started, it is implicitly connected to the default application
server. The application process can explicitly connect to a different
application server by issuing a CONNECT TO statement. A connection
lasts until a CONNECT RESET statement or a DISCONNECT statement is issued or
until another CONNECT TO statement changes the application server.
See Remote Unit of Work Connection Management for concepts and additional details on connection
states. See Options that Govern Distributed Unit of Work Semantics for the precompiler options that determine the
framework for CONNECT behavior.
Invocation
Although an interactive SQL facility might provide an interface that gives
the appearance of interactive execution, this statement can only be embedded
within an application program. It is an executable statement that
cannot be dynamically prepared.
Authorization
The authorization ID of the statement must be authorized to connect to the
identified application server. Depending on the authentication setting
for the database, the authorization check may be performed by either the
client or the server. For a partitioned database, the user and group
definitions must be identical across partitions or nodes. Refer to the
AUTHENTICATION database manager configuration parameter in the Administration Guide for information about the authentication setting.
Syntax
>>-CONNECT------------------------------------------------------>
>-----+----------------------------------------------------------------------+>
+-TO--+-server-name---+---+-----------------+---+--------------------+-+
| '-host-variable-' '-| lock-block |--' '-| authorization |--' |
+-RESET----------------------------------------------------------------+
| (1) |
'-| authorization |----------------------------------------------------'
>--------------------------------------------------------------><
authorization
|---USER----+-authorization-name-+--USING----+-password------+-->
'-host-variable------' '-host-variable-'
>-----+---------------------------------------------+-----------|
'-NEW--+-password------+---CONFIRM--password--'
'-host-variable-'
lock-block
.-IN SHARE MODE--------------------------.
|---+----------------------------------------+------------------|
'-IN EXCLUSIVE MODE--+----------------+--'
'-ON SINGLE NODE-'
Notes:
- This form is only valid if implicit connect is enabled.
Description
- CONNECT (with no operand)
- Returns information about the current server. The information is
returned in the SQLERRP field of the SQLCA as described in "Successful
Connection".
If a connection state exists, the authorization ID and database alias are
placed in the SQLERRMC field of the SQLCA. If the authorization ID is
longer than 8 bytes, it will be truncated to 8 bytes, and the truncation will
be flagged in the SQLWARN0 and SQLWARN1 fields of the SQLCA, with 'W'
and 'A', respectively. If the database configuration parameter
DYN_QUERY_MGMT is enabled, then the SQLWARN0 and SQLWARN7 fields of the SQLCA
will be flagged with 'W' and 'E', respectively.
If no connection exists and implicit connect is possible, then an attempt
to make an implicit connection is made. If implicit connect is not
available, this attempt results in an error (no existing connection).
If no connection, then the SQLERRMC field is blank.
The country code and code page of the application server are placed in the
SQLERRMC field (as they are with a successful CONNECT TO statement).
This form of CONNECT:
- Does not require the application process to be in the connectable
state.
- If connected, does not change the connection state.
- If unconnected and implicit connect is available, a connection to the
default application server is made. In this case, the country code and
code page of the application server are placed in the SQLERRMC field, like a
successful CONNECT TO statement.
- If unconnected and implicit connect is not available, the application
process remains unconnected.
- Does not close cursors.
- TO server-name or host-variable
- Identifies the application server by the specified server-name or
a host-variable which contains the server-name.
If a host-variable is specified, it must be a character string
variable with a length attribute that is not greater than 8, and it must not
include an indicator variable. The server-name that is
contained within the host-variable must be left-justified and must
not be delimited by quotation marks.
Note that the server-name is a database alias identifying the
application server. It must be listed in the application
requester's local directory.
Note: | DB2 for MVS supports a 16 byte location-name and both SQL/DS and DB2/400
support a 18 byte target database name. DB2 Version 7 only supports the
use of 8 byte database-alias name on the SQL CONNECT statement.
However, the database-alias name can be mapped to an 18 byte database name
through the Database Connection Service Directory.
|
When the CONNECT TO statement is executed, the application process must be
in the connectable state (see Remote Unit of Work Connection Management for information about connection states with Type 1
CONNECT).
Successful Connection:
If the CONNECT TO statement is successful:
- All open cursors are closed, all prepared statements are destroyed, and
all locks are released from the previous application server.
- The application process is disconnected from its previous application
server, if any, and connected to the identified application server.
- The actual name of the application server (not an alias) is placed in the
CURRENT SERVER special register.
- Information about the application server is placed in the SQLERRP field of
the SQLCA. If the application server is an IBM product, the information
has the form pppvvrrm, where:
- ppp identifies the product as follows:
- DSN for DB2 for MVS
- ARI for SQL/DS
- QSQ for DB2/400
- SQL for DB2 Universal Database
- vv is a two-digit version identifier such as '02'
- rr is a two-digit release identifier such as '01'
- m is a one-digit modification level identifier such as
'0'.
For example, if the application server is Version 1 Release 1 of DB2 for
OS/2, the value of SQLERRP is 'SQL01010'.
66
- The SQLERRMC field of the SQLCA is set to contain the following values
(separated by X'FF')
- the country code of the application server (or blanks if using DDCS),
- the code page of the application server (or CCSID if using DDCS),
- the authorization ID (up to first 8 bytes only),
- the database alias,
- the platform type of the application server. Currently identified
values are:
- Token
- Server
- QAS
- DB2 Universal Database for AS/400
- QDB2
- DB2 Universal Database for OS/390
- QDB2/2
- DB2 Universal Database for OS/2
- QDB2/6000
- DB2 Universal Database for AIX
- QDB2/HPUX
- DB2 Universal Database for HP-UX
- QDB2/LINUX
- DB2 Universal Database for Linux
- QDB2/NT
- DB2 Universal Database for Windows NT
- QDB2/PTX
- DB2 Universal Database for NUMA-Q
- QDB2/SCO
- DB2 Universal Database for SCO UnixWare
- QDB2/SNI
- DB2 Universal Database for Siemens Nixdorf
- QDB2/SUN
- DB2 Universal Database for Solaris Operating System
- QDB2/Windows 95
- DB2 Universal Database for Windows 95 or Windows 98
- QSQLDS/VM
- DB2 Server for VM
- QSQLDS/VSE
- DB2 Server for VSE
- The agent ID. It identifies the agent executing within the database
manager on behalf of the application. This field is the same as the
agent_id element returned by the database monitor.
- The agent index. It identifies the index of the agent and is used
for service.
- Partition number. For a non-partitioned database, this is always 0,
if present.
- The code page of the application client.
- Number of partitions in a partitioned database. If the database
cannot be partitioned, the value is 0 (zero). Token is present only
with Version 5 or later.
- The SQLERRD(1) field of the SQLCA indicates the maximum expected
difference in length of mixed character data (CHAR data types) when converted
to the database code page from the application code page. A value of 0
or 1 indicates no expansion; a value greater than 1 indicates a possible
expansion in length; a negative value indicates a possible
contraction. 67
- The SQLERRD(2) field of the SQLCA indicates the maximum expected
difference in length of mixed character data (CHAR data types) when converted
to the application code page from the database code page. A value of 0
or 1 indicates no expansion; a value greater than 1 indicates a possible
expansion in length; a negative value indicates a possible
contraction. 67
- The SQLERRD(3) field of the SQLCA indicates whether or not the database on
the connection is updatable. A database is initially updatable, but is
changed to read-only if a unit of work determines the authorization ID cannot
perform updates. The value is one of:
- 1 - updatable
- 2 - read-only
- The SQLERRD(4) field of the SQLCA returns certain characteristics of the
connection. The value is one of:
- 0 -
- N/A (only possible if running from a down-level client which is one phase
commit and is an updater).
- 1 -
- one-phase commit.
- 2 -
- one-phase commit; read-only (only applicable to connections to DRDA1
databases in TP Monitor environment).
- 3 -
- two-phase commit.
- The SQLERRD(5) field of the SQLCA returns the authentication type of the
connection. The value is one of:
- 0 -
- Authenticated on the server.
- 1 -
- Authenticated on the client.
- 2 -
- Authenticated using DB2 Connect.
- 3 -
- Authenticated using Distributed Computing Environment security
services.
- 255 -
- Authentication not specified.
See "Controlling Database Access" in the Administration
Guide for details on authentication types.
- The SQLERRD(6) field of the SQLCA returns the partition number of the
partition to which the connection was made if the database is
partitioned. Otherwise, a value of 0 is returned.
- The SQLWARN1 field in the SQLCA will be set to 'A' if the
authorization ID of the successful connection is longer than 8 bytes.
This indicates that truncation has occurred. The SQLWARN0 field in the
SQLCA will be set to 'W' to indicate this warning.
- The SQLWARN7 field in the SQLCA will be set to 'E' if the database
configuration parameter DYN_QUERY_MGMT for the database is enabled. The
SQLWARN0 field in the SQLCA will be set to 'W' to indicate this
warning.
Unsuccessful Connection:
If the CONNECT TO statement is unsuccessful:
- The SQLERRP field of the SQLCA is set to the name of the module at the
application requester that detected the error. Note that the first
three characters of the module name identifies the product. For
example, if the application requester is on the OS/2 database manager, the
first three characters are 'SQL'.
- If the CONNECT TO statement is unsuccessful because the application
process is not in the connectable state, the connection state of the
application process is unchanged.
- If the CONNECT TO statement is unsuccessful because the
server-name is not listed in the local directory, an error message
(SQLSTATE 08001) is issued and the connection state of the application process
remains unchanged:
- If the application requester was not connected to an application server
then the application process remains unconnected.
- If the application requester was already connected to an application
server, the application process remains connected to that application
server. Any further statements are executed at that application
server.
- If the CONNECT TO statement is unsuccessful for any other reason, the
application process is placed into the unconnected state.
- IN SHARE MODE
- Allows other concurrent connections to the database and prevents other
users from connecting to the database in exclusive mode.
- IN EXCLUSIVE MODE 68
- Prevents concurrent application processes from executing any operations at
the application server, unless they have the same authorization ID as the user
holding the exclusive lock.
- ON SINGLE NODE
- Specifies that the coordinator partition is connected in exclusive mode
and all other partitions are connected in share mode. This option is
only effective in a partitioned database.
- RESET
- Disconnects the application process from the current server. A
commit operation is performed. If implicit connect is available, the
application process remains unconnected until an SQL statement is
issued.
- USER authorization-name/host-variable
- Identifies the userid trying to connect to the application server.
If a host-variable is specified, it must be a character string variable with a
length attribute that is not greater than 8, and it must not include an
indicator variable. The userid that is contained within the
host-variable must be left justified and must not be delimited by
quotation marks.
- USING password/host-variable
- Identifies the password of the userid trying to connect to the application
server. Password or host-variable may be up to 18
characters. If a host variable is specified, it must be a character
string variable with a length attribute not greater than 18 and it must not
include an indicator variable.
- NEW password/host-variable CONFIRM password
- Identifies the new password that should be assigned to the userid
identified by the USER option. Password or
host-variable may be up to 18 characters. If a host variable
is specified, it must be a character string variable with a length attribute
not greater than 18 and it must not include an indicator variable. The
system on which the password will be changed depends on how user
authentication is set up.
Notes
- It is good practice for the first SQL statement executed by an application
process to be the CONNECT TO statement.
- If a CONNECT TO statement is issued to the current application server with
a different userid and password then the conversation is deallocated and
reallocated. All cursors are closed by the database manager (with the
loss of the cursor position if the WITH HOLD option was used).
- If a CONNECT TO statement is issued to the current application server with
the same userid and password then the conversation is not deallocated and
reallocated. Cursors, in this case, are not closed.
- To use DB2 Universal Database Enterprise - Extended Edition, the user or
application must connect to one of the partitions listed in the
db2nodes.cfg file (see Data Partitioning Across Multiple Partitions for information about this file). You
should try to ensure that not all users use the same partition as the
coordinator partition.
Examples
Example 1: In a C program, connect to the application
server TOROLAB3, where TOROLAB3 is a database alias of the same name, with the
userid FERMAT and the password THEOREM.
EXEC SQL CONNECT TO TOROLAB3 USER FERMAT USING THEOREM;
Example 2: In a C program, connect to an application
server whose database alias is stored in the host variable APP_SERVER
(varchar(8)). Following a successful connection, copy the 3 character
product identifier of the application server to the variable PRODUCT
(char(3)).
EXEC SQL CONNECT TO :APP_SERVER;
if (strncmp(SQLSTATE,'00000',5))
strncpy(PRODUCT,sqlca.sqlerrp,3);
Footnotes:
- 66
-
This release of DB2 Universal Database Version 7 is
'SQL07010'.
- 67
-
See the "Character Conversion Expansion Factor" section of the
"Programming in Complex Environments" chapter in the Application Development Guide for details.
- 68
-
This option is not supported by DDCS.
[ Top of Page | Previous Page | Next Page ]