SQL Keywords

SQL CONNECT

What is the SQL CONNECT statement?

CONNECT opens a new session to a target database from within an SQL script or interactive client.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL CONNECT:

SQL CONNECT Full Explanation

The CONNECT statement establishes a logical session between the client application and a specific database or database server. After a successful CONNECT, subsequent SQL statements are executed in the context of that session until it is explicitly terminated with DISCONNECT or the application ends. The statement is most common in IBM DB2, Informix, and other ANSI/ISO-compliant CLI environments, where it is part of the language rather than merely a client-side command. CONNECT does not modify data; it only authenticates the user, allocates resources, and sets the current database, schema, and default settings such as isolation level. Some systems allow multiple concurrent connections; others permit only one active CONNECT at a time. Because credentials travel over the network, use encrypted channels (TLS/SSL) or parameterized connection objects to avoid leaking sensitive information. CONNECT is not available in MySQL or PostgreSQL SQL grammar but has equivalent client commands (e.g., \connect in psql).

SQL CONNECT Syntax

CONNECT TO database_name
    [USER user_name]
    [USING 'password'];

SQL CONNECT Parameters

  • user_name - STRING Login or authorization ID to authenticate (optional if using OS credentials)
  • password - STRING Plain-text or parameterized password associated with user_name

Example Queries Using SQL CONNECT

-- Simple connection in DB2
CONNECT TO salesdb;

-- Connection with explicit user and password in Informix
CONNECT TO marketingdb USER analyst USING 's3cur3P@ss';

-- Switch to another database within the same session (DB2 supports multiple concurrent connections)
CONNECT TO hrdb USER hr_admin USING :hr_password;

Expected Output Using SQL CONNECT

  • The server authenticates the credentials, allocates a new connection handle, and returns a message such as "Database connection information
  • Connection state = CONNECTED"
  • Subsequent SQL statements run against the specified database

Use Cases with SQL CONNECT

  • Initiating a database session from an SQL script executed by a scheduler
  • Switching between multiple databases inside a single batch job
  • Authenticating as a different user to test role-based permissions
  • Opening a connection before running data-load or reporting procedures

Common Mistakes with SQL CONNECT

  • Omitting USER/USING when the server requires explicit credentials
  • Attempting CONNECT on systems where only one active connection is allowed without first issuing DISCONNECT
  • Confusing client-side commands (\connect, CONNECT command in SQL*Plus) with SQL-level CONNECT; the syntax and capabilities differ
  • Hard-coding passwords directly in scripts, leading to security risks

Related Topics

First Introduced In

IBM DB2 Version 1 (1983) and later formalized in the ANSI SQL/CLI (SQL-92) standard

Frequently Asked Questions

What is the difference between CONNECT and DISCONNECT?

CONNECT opens a new session to a database, while DISCONNECT cleanly terminates it, releasing server and client resources.

Does CONNECT work in PostgreSQL and MySQL?

No. Those systems expose connection switching as client commands (\connect and \r in psql, \connect in the mysql client) but do not include CONNECT in their SQL grammar.

How do I avoid putting passwords in plain text?

Pass credentials through environment variables, encrypted wallets, or parameter markers (host variables) rather than hard-coding them inside scripts.

Can I run CONNECT inside a stored procedure?

Most databases prohibit CONNECT within stored procedures for security and transactional integrity reasons. Always check your vendor documentation.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!