How To Declare Variable In Sql

4 min read

Declaring a variable in SQL is one of the first skills that separates beginners from confident query writers. A variable lets you store a value temporarily, reuse it across multiple statements, and make your SQL easier to read and maintain. The exact syntax for how to declare a variable in SQL depends on the database system you use, because SQL is a standard rather than a single product. That's why sQL Server, MySQL, PostgreSQL, Oracle, and SQLite all handle variables differently. This guide walks through the most common approaches, explains scope and data types, and highlights mistakes that trip up new developers.

What Does It Mean to Declare a Variable in SQL?

A variable is a named placeholder for a value. Think about it: in SQL, variables are usually used inside scripts, stored procedures, functions, or analytical queries. They can hold numbers, text, dates, and other data types.

  • The variable’s name, such as @total_sales or customer_count.
  • The data type, such as INT, VARCHAR(100), or DATE.
  • An optional initial value, which may be assigned immediately or later.

Not every SQL environment supports variables in the same way. Some databases allow variables only inside procedural code, while others support session-level variables that persist until the connection closes. Understanding that distinction prevents many frustrating errors No workaround needed..

How to Declare a Variable in SQL Server

SQL Server uses the DECLARE statement with an @ prefix. The basic syntax is:

DECLARE @variable_name data_type;
DECLARE @variable_name data_type = initial_value;

For example:

DECLARE @customer_count INT;
SET @customer_count = 25;

DECLARE @order_total DECIMAL(10,2) = 199.99;

You can also assign a value with SELECT:

DECLARE @city VARCHAR(50);
SELECT @city = 'London';

In SQL Server, variables declared with DECLARE are scoped to the current batch or stored procedure. They are not permanent database objects. A common mistake is using SELECT to assign a value from a query that returns multiple rows. Which means in that case, SQL Server may assign only one value unpredictably. Use SET for simple assignments and be careful when populating a variable from a query Took long enough..

How to Declare a Variable in MySQL

MySQL has two common variable types: user-defined session variables and local variables inside stored programs.

Session variables use the @ symbol and do not require a data type declaration:

SET @total_orders = 100;
SELECT @total_orders;

You can also assign a value with SELECT ... INTO:

SELECT COUNT(*) INTO @row_count FROM orders;

Local variables are used inside stored procedures, functions, and triggers. They must be declared at the beginning of a BEGIN ... END block:

DELIMITER //

CREATE PROCEDURE example_proc()
BEGIN
    DECLARE customer_total INT DEFAULT 0;
    SELECT COUNT(*) INTO customer_total FROM customers;
    SELECT customer_total;
END //

DELIMITER ;

MySQL local variables require a data type and can include a DEFAULT value. That's why they are scoped to the block where they are declared. Session variables, by contrast, last for the duration of the database connection The details matter here..

How to Declare a Variable in PostgreSQL

PostgreSQL does not support plain SQL variables in the same way SQL Server does. Outside procedural code, you typically use psql variables or a DO block. Inside PL/pgSQL, variables are declared in a DECLARE section:

DO $
DECLARE
    total_sales NUMERIC := 0;
    customer_name TEXT := 'Alice';
BEGIN
    SELECT SUM(amount) INTO total_sales FROM sales;
    RAISE NOTICE 'Total: %', total_sales;
END $;

The := operator assigns a value. PostgreSQL variables are local to the block or function. For reusable logic, define a function:

CREATE FUNCTION get_total_sales() RETURNS NUMERIC AS $
DECLARE
    total NUMERIC;
BEGIN
    SELECT SUM(amount) INTO total FROM sales;
    RETURN total;
END;
$ LANGUAGE plpgsql;

If you are writing a simple query in psql, you can use \set to define a client-side variable, but that is a psql feature rather than a PostgreSQL server feature.

How to Declare a Variable in Oracle PL/SQL

Oracle uses PL/SQL blocks. A variable is declared in the DECLARE section before BEGIN:

DECLARE
    v_total NUMBER := 0;
    v_name VARCHAR2(100) := 'Oracle User';
BEGIN
    SELECT SUM(amount) INTO v_total FROM sales;
    DBMS_OUTPUT.PUT_LINE('Total: ' || v_total);
END;

Oracle variables are scoped to the PL/SQL block. Now, you can also declare variables inside stored procedures, functions, and packages. The assignment operator is :=, and the INTO clause is used to capture query results Easy to understand, harder to ignore..

How to Declare a Variable in SQLite

SQLite does not have a native DECLARE statement for general SQL scripts. It supports parameter binding in application code, and it allows temporary tables or common table expressions (CTEs) to achieve

Still Here?

Hot Topics

Along the Same Lines

Similar Reads

Thank you for reading about How To Declare Variable In Sql. 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