Lesson 30. Variables
We are now at the point where you understand how to pull data from tables. It is time to start learning how to actually program SQL Server so you can do things like write processes that pull, manipulate, and load data.
Variables only live as long as the script executes. When the script is done, the variable is wiped from memory.
The first step is learning how to create variables and assign them values. You create variables with DECLARE and set their value with SET. When setting your variable, string values have to be enclosed in quotes just like when you were writing WHERE clauses.
DECLARE Syntax
In [ ]:
SET Syntax
In [ ]:
Examples
Declaring A Variable
In [ ]:
Setting A Variable Value
In [ ]:
Declaring A Default Value
In [ ]:
Viewing The Value Of A Variable
In [ ]:
Set The Value Of Variables With The Output Of A SQL Statement
The key is the SQL statement has to return a single atomic value.
In [ ]:
Variable Usage
Variables can be used in various ways. You can use them for output in SELECT statements and in WHERE clauses to create dynamic filter criteria.
In [ ]:
Last updated