How To Declare Variable In Sql Query

2 min read

Variables are one of the most practical tools in SQL, yet they confuse many beginners because the syntax changes dramatically from one database engine to another. Learning how to declare a variable in an SQL query means understanding that SQL has no single universal DECLARE statement — each platform (SQL Server, MySQL, PostgreSQL, Oracle, SQLite) implements variables differently, and some do not support them in plain queries at all. This guide walks through every major dialect with working examples, explains when to use variables instead of hard-coded values, and covers the mistakes that quietly break queries in production Simple, but easy to overlook..

Why Use Variables in SQL at All?

A variable is a named placeholder that stores a value so you can reuse it without repeating yourself. In practice, variables help you:

  • Avoid repetition — write a date filter once and reference it in five queries.
  • Improve readability — @StartDate is clearer than '2024-01-01 00:00:00' scattered everywhere.
  • Support dynamic logic — store a computed total, then use it in a second query.
  • Simplify testing — change one value at the top of a script instead of hunting through the code.

The catch is scope. On the flip side, a variable might live for a single batch, an entire session, or only inside a stored procedure. Knowing which one you are dealing with prevents a lot of head-scratching Took long enough..

Declaring Variables in SQL Server (T-SQL)

SQL Server has the most straightforward syntax. You use DECLARE, and local variables are prefixed with @ The details matter here..

DECLARE @TotalSales DECIMAL(10,2);
SET @TotalSales = 1500.00;

DECLARE @StartDate DATE = '2024-01-01';
DECLARE @Name NVARCHAR(50) = N'Alice', @Age INT = 30;

You can declare and assign in one line, and you can declare several variables in a single statement separated by commas.

SET vs SELECT for Assignment

SQL Server gives you two ways to assign a value:

SET @TotalSales = (SELECT SUM(Amount) FROM Sales WHERE SaleDate >= @StartDate);

SELECT @TotalSales = SUM(Amount) FROM Sales WHERE SaleDate >= @StartDate;
  • SET is the ANSI-standard form and assigns exactly one variable at a time. It is the safer, more predictable choice.
  • SELECT can assign multiple variables in one statement, but if the query returns no rows, the variable silently keeps its previous value instead of becoming NULL. That behavior causes subtle bugs, so prefer SET unless you specifically need multi-variable assignment.

Table Variables

SQL Server also lets you declare a variable that holds an entire result set:

DECLARE @Orders TABLE (OrderID INT, CustomerName NVARCHAR(100));
INSERT INTO @Orders VALUES (1, 'Alice'), (2, 'Bob');
SELECT * FROM @Orders;

Table variables are handy for small intermediate datasets, though temporary tables (#Temp) generally perform better once row counts grow Took long enough..

Declaring Variables in MySQL

MySQL has two distinct variable systems, and mixing them up is a common source of errors The details matter here..

User-Defined Variables (Session Scope)

These use the @

Out Now

Coming in Hot

Related Territory

Cut from the Same Cloth

Thank you for reading about How To Declare Variable In Sql Query. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home