SQL Keywords

SQL DISCONNECT

What does the SQL DISCONNECT statement do?

DISCONNECT ends one or more existing database connections and frees the associated resources.
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 DISCONNECT:

SQL DISCONNECT Full Explanation

DISCONNECT is a control statement defined in the SQL standard for clients that can maintain multiple open sessions at once. It cleanly terminates a specified connection, the current connection, or all connections. Before the session is closed, the database performs an implicit COMMIT of any outstanding transaction, thereby making all changes permanent (some vendors instead perform an implicit ROLLBACK – always verify). After a connection is disconnected, any cursor, prepared statement, or session state tied to it becomes invalid. Attempting to issue further SQL statements on that connection raises an error until a new CONNECT statement is executed. DISCONNECT does not shut down the database server itself; it only affects the client session(s).

SQL DISCONNECT Syntax

DISCONNECT [ connection_name | CURRENT | ALL ];

SQL DISCONNECT Parameters

  • connection_name (identifier) - Name of an open connection established with CONNECT
  • CURRENT (keyword) - Closes only the active connection
  • ALL (keyword) - Closes every open connection held by the client

Example Queries Using SQL DISCONNECT

-- Open two sessions
CONNECT TO sales_db AS sales;
CONNECT TO hr_db    AS hr;

-- Work on the sales connection
SET CONNECTION sales;
UPDATE orders SET status = 'shipped' WHERE id = 42;

-- Close just the sales session
DISCONNECT sales;

-- Later, close whatever connection is currently active
DISCONNECT CURRENT;

-- Finally, ensure no sessions remain
DISCONNECT ALL;

Expected Output Using SQL DISCONNECT

  • The targeted session(s) are closed, any pending work is implicitly committed (vendor dependent), and all related cursors and statements are released
  • Subsequent SQL on those sessions raises an error until reconnected

Use Cases with SQL DISCONNECT

  • Cleaning up idle sessions in a multi-connection client
  • Ensuring resources are released in long-running scripts
  • Explicitly committing work then detaching from the database
  • Closing all sessions at script termination to avoid leaks

Common Mistakes with SQL DISCONNECT

  • Assuming DISCONNECT rolls back work when the vendor actually commits it
  • Attempting to DISCONNECT when only one implicit connection exists (many tools auto-connect and ignore DISCONNECT)
  • Misspelling the connection name, resulting in “connection does not exist” errors

Related Topics

First Introduced In

SQL-92 (ISO/IEC 9075:1992)

Frequently Asked Questions

Does DISCONNECT commit or rollback transactions?

Most engines commit pending work before closing the session, but a few roll back. Verify your database to avoid surprises.

How do I close only my current connection?

Use DISCONNECT CURRENT. The command terminates the active session while leaving others open.

What happens to cursors after DISCONNECT?

All cursors, prepared statements, and session context tied to that connection are automatically dropped and become unusable.

Is DISCONNECT supported in PostgreSQL psql?

No. psql uses the meta-command \q to exit the client. DISCONNECT applies to standards-compliant multi-connection environments or embedded SQL.

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!