Explain Codes LogoExplain Codes Logo

How to Execute SQL Query without Displaying Results

sql
result-suppression
sql-commands
database-performance
Alex KataevbyAlex Kataev·Dec 9, 2024
TLDR

To quietly run an SQL query, sandwich your code between SET NOCOUNT ON; and SET NOCOUNT OFF;. This mutes the typical feedback indicating the number of rows impacted.

Example:

SET NOCOUNT ON; -- Code clause -- No comments about rows from me, I'm on break! INSERT INTO Table (Column) VALUES ('Data'); SET NOCOUNT OFF;

This method doesn't affect the execution or output of the queries, it simply turns off the row count messages.

Effective Result Suppression Tactics

Silence Alterations

In cases where you don’t need feedback, like inserts or updates, enveloping them with SET NOCOUNT ON helps make operations more efficient and less noisy.

Execute Plans without Data Distraction

The "Discard results after execution" option in SQL Server Management Studio (SSMS) allows you to acquire the Actual Execution Plan without clogging your screen with returned results.

Preserving System Resources

Choosing not to load results can save network bandwidth and memory. It’s the equivalent of going to the library, getting your book, and not stopping to browse every other title (tempting as it may be).

Reduce Traffic with "Discard results"

If the screen says "too many result sets", it’s like a traffic jam. Use the "discard result after execution" option in SSMS as your traffic cop, and get things moving smoothly again.

Silencing Results: The Alternative Approaches

Table Structure Check: SET FMTONLY

SET FMTONLY ON used to return metadata without data. It’s now deprecated, and may return an empty recordset structure. Make sure to use it wisely.

Syntax Check Only: SET NOEXEC

By setting SET NOEXEC ON, the query will be compiled and checked for syntax errors without being executed. Handy for testing your SQL spelling!

Side-effect Operations

In some operations, it’s just about the side effects, like a medicine that's supposed to relieve pain but also makes you drowsy. Suppressing result sets focuses your attention on the side effects.

Knowing What’s Changed: SET NOCOUNT

Understanding how to use SET commands such as SET NOCOUNT ON/OFF can yield big payoffs by helping you fine-tune result visibility.

Visualization

Reflect on an SQL query as a hard-working ninja (🥷):

SET NOCOUNT ON; -- Ninja begins mission -- Wielding SQL chakra! SET NOCOUNT OFF;

This ninja completes the assignment under the radar (🌫️) and gets out without giving away any secrets.

Start: [📂📂📂] Finish: [📂📂📂] // No trace!

Key Idea: The ninja (database) completes the mission (command), but keeps the secrets (results) to itself.

Fine-tuning Command Execution

The Role of SET Commands

Understanding the roles of various SET commands helps create an optimal strategy for suppressing unwanted result displays.

  • SET NOCOUNT ON: No more will there be an obsession with count.
  • SET FMTONLY ON/OFF: Lets you decide whether to return the actual data or just the metadata.
  • SET NOEXEC ON/OFF: Compiles the query but doesn’t execute it, handy for syntax checks.

Instances for Silent Execution

It’s not always necessary to visually verify every result set. Here are some common scenarios:

  • Batch processes: You’re handling a ton of records and don't need immediate results.
  • Data Migration: Carrying hefty data loads where an immediate data check isn’t crucial.
  • Performance tuning: You’re running tests and actual data isn’t essential.