@ and @@ in SQL — Meaning, Difference, and Best Use

If you’ve been learning SQL and came across the symbols @ and @@, you might have wondered:
1. What is @ and @@ in SQL?
2. Are they the same?
3. Which is better: @ or @@ in SQL?

Let’s clear up all the confusion in a simple, teacher-style explanation — with easy examples you can try right now.


Introduction: Why @ and @@ Matter in SQL

In SQL Server (Microsoft T-SQL), both @ and @@ are used to refer to variables or system-level values.
However, they serve very different purposes:

  • @ → Used for user-defined variables.
  • @@ → Used for built-in system functions or global variables.

Understanding the difference between @ and @@ in SQL is essential for writing clean, bug-free queries and passing SQL interviews with confidence.


What is @ in SQL?

The @ symbol is used to declare and use local variables within your SQL scripts, stored procedures, or functions.

Example:

DECLARE @StudentName NVARCHAR(50);
SET @StudentName = 'Alex';

SELECT @StudentName AS Student;

Explanation:

  • DECLARE creates a variable named @StudentName.
  • SET assigns a value to it.
  • SELECT displays its current value.

Use @ when you need to store temporary data like counters, IDs, or calculations inside your SQL script.


What is @@ in SQL?

The @@ symbol is used for system functions (global variables) that return information about the SQL Server environment, configuration, or current session.

These variables are read-only — you can’t change their values.

Example:

SELECT @@VERSION AS SQLServerVersion;

Explanation:

  • @@VERSION returns the SQL Server build and version details.

Other popular @@ variables include:

System VariableDescription
@@ERRORReturns the error number for the last executed statement.
@@ROWCOUNTReturns the number of rows affected by the last command.
@@IDENTITYReturns the last generated identity value.
@@SERVERNAMEReturns the name of the current server.

Difference Between @ and @@ in SQL

Feature@@@
TypeUser-defined variableSystem-defined global variable
Can declare?Yes (DECLARE @VarName)No (predefined by SQL Server)
Can modify?YesNo
ScopeLocal (procedure, batch, or function)Global (SQL Server session or system)
Example@Name, @Age, @Total@@ROWCOUNT, @@ERROR, @@VERSION

Quick Tip:

Use @ for your own variables and @@ to access SQL Server’s internal values.


Which is Better: @ or @@ in SQL?

Neither is “better” — they serve different purposes.

  • Use @ when you want to store and manipulate your own data temporarily.
  • Use @@ when you want SQL Server to give you information (like system stats or environment values).

Example combining both:

DECLARE @RowsInserted INT;

INSERT INTO Students (Name) VALUES ('Liam');

SET @RowsInserted = @@ROWCOUNT;

SELECT @RowsInserted AS 'Number of Rows Inserted';

✅ Here:

  • @@ROWCOUNT gives the number of rows affected.
  • The result is stored in @RowsInserted, your user variable.


    @ and @@ in SQL Examples (Quick Recap)

    TaskExampleExplanation
    Declare a variableDECLARE @Count INTCreates a local variable
    Set a variableSET @Count = 5Assigns value to variable
    Get SQL Server versionSELECT @@VERSIONReturns SQL Server version
    Get affected rowsSELECT @@ROWCOUNTNumber of rows changed by last query

    Summary

    Here’s what you should remember:

    • @ → For your own variables (local).
    • @@ → For SQL Server’s built-in variables (system/global).
    • You declare and set @ variables.
    • You read @@ variables; you can’t change them.
    • Both are essential for writing professional SQL scripts and debugging queries efficiently.

    SQL Interview Questions on @ and @@

    1. Question 1:
      What is the difference between @ and @@ in SQL Server? Give one example of each.
    2. Question 2:
      Write a SQL query that inserts a record into a table, captures the number of affected rows using @@ROWCOUNT, and displays it using a variable.

    Final Note

    If you’re preparing for SQL interviews or writing stored procedures, understanding the @ vs @@ difference in SQL is a small detail that makes a big difference.
    It shows you understand both the user logic and the system internals — a skill every good SQL developer needs!

    javaconcurrency

    Best Java Book On Amazon

    Leave a Comment

    Your email address will not be published. Required fields are marked *