What are variables, data types, and control flow statements in T-SQL?
Updated Feb 20, 2026
Short answer
Variables store temporary values, data types define what kind of values T-SQL can handle, and control flow statements decide the order in which SQL statements execute. Together, they let stored procedures, scripts, and batches perform logic instead of only running fixed queries.
Deep explanation
In T-SQL, variables, data types, and control flow are the building blocks for writing programmable database logic. A query retrieves or changes data, but these features allow a script to remember values, validate conditions, repeat actions, and respond to different situations.
Variables: storing temporary values
A variable is a named memory location used to hold a value during execution. In T-SQL, local variables begin with @ and must be declared with a data type before use.
Common operations are:
DECLAREcreates a variable.SETassigns a value.SELECTcan also assign values from query results.
Example:
DECLARE @CustomerCount INT;
SET @CustomerCount = 100;
SELECT @CustomerCount AS TotalCustomers;Variables exist only during the current session, batch, or stored procedure execution. They are useful for calculations, temporary storage, and passing values between statements.
A variable has a name, but its data type determines what values it can safely store.
Data types: defining the shape of data
A data type tells SQL Server what kind of value a column or variable contains. Choosing the correct type improves storage efficiency, accuracy, and query behavior.
Common T-SQL data types include:
| Category | Examples | Used for |
|---|---|---|
| Numeric | INT, DECIMAL, FLOAT | Counts, prices, measurements |
| Character | CHAR, VARCHAR, NVARCHAR | Text values |
| Date and time | DATE, DATETIME2 | Dates and timestamps |
| Logical | BIT | True/false style values |
For example, a product price should usually use DECIMAL instead of FLOAT because exact financial calculations require predictable precision.
Control flow: controlling execution order
Control flow statements change the normal top-to-bottom execution path of a script. They allow decisions and repetition.
Common statements include:
IF...ELSEfor conditional execution.WHILEfor loops.BEGIN...ENDfor grouping multiple statements.CASEfor conditional expressions inside queries.TRY...CATCHfor error handling.
The relationship between these concepts can be visualized as:
A typical execution pattern is:
DECLARE @OrderTotal DECIMAL(10,2);
SET @OrderTotal = 250.00;
IF @OrderTotal > 100BEGIN PRINT 'Large order';ENDELSEBEGIN PRINT 'Standard order';END;Control flow does not change the data itself; it changes which instructions run and when they run.
Why interviewers care
Interviewers usually look for whether a candidate understands that T-SQL is not only a data retrieval language. Programmable features make database logic reusable and predictable.
A strong answer connects these ideas:
- Variables temporarily store state.
- Data types protect that state by defining valid values.
- Control flow uses that state to make decisions.
⚠️ Use the simplest data type and control structure that correctly represents the problem. Overly complex logic inside the database can become difficult to maintain.
Real-world example
Imagine an order-processing procedure that applies a discount only for large purchases. The procedure stores the order amount in a variable, checks the value using control flow, and calculates the final price.
DECLARE @OrderAmount DECIMAL(10,2);DECLARE @Discount DECIMAL(5,2);
SET @OrderAmount = 500.00;
IF @OrderAmount >= 500 SET @Discount = 10.00;ELSE SET @Discount = 0.00;
SELECT @OrderAmount, @Discount;Here, DECIMAL ensures money values are accurate, variables hold temporary calculation results, and IF controls which discount rule executes.
Good T-SQL code combines the right data types with clear control flow rather than hiding complicated business rules in unclear scripts.
Common mistakes
- * **Wrong data type** - Using a type like `FLOAT` for money can create precision problems
- choose types based on the data's purpose.
- * **Uninitialized variables** - Declaring a variable without assigning a value can produce unexpected `NULL` results
- set values explicitly.
- * **Missing `BEGIN...END`** - Writing multiple statements after `IF` without grouping can cause only the first statement to be controlled.
- * **Excessive loops** - Using `WHILE` for large data operations can be slow
- prefer set-based SQL operations when possible.
- * **Confusing variables and columns** - Variables store temporary execution values, while columns store table data permanently.
Follow-up questions
- How is a local variable different from a table column?
- What happens when a variable is declared but not assigned a value?
- When should you use IF versus CASE in T-SQL?
- Why are set-based operations usually preferred over WHILE loops in SQL?