WHILE Loops: Ginger Grant
WHILE Loops: Ginger Grant
I N T E R M E D I AT E S Q L S E R V E R
Ginger Grant
Instructor
Using variables in T-SQL
Variables are needed to set values DECLARE @variablename data_type
Must start with the character @
s : number of decimal digits that will be stored to the right of the decimal point
+--------------------+ +--------------------+
|(No column name) | |(No column name) |
+--------------------+ +--------------------+
|Cookies | |Candy |
+--------------------+ +--------------------+
After the WHILE, there should be a line with the keyword BEGIN
Next include code to run until the condition in the WHILE loop is true
+--------------------+
|(No column name) |
+--------------------+
|10 |
+--------------------+
Ginger Grant
Instructor
What are Derived tables?
Query which is treated like a temporary table
Can contain intermediate calculations to be used the main query or different joins than in the main
query
Ginger Grant
Instructor
CTE syntax
-- CTE definitions start with the keyword WITH
-- Followed by the CTE names and the columns it contains
WITH CTEName (Col1, Col2)
AS
-- Define the CTE query
(
-- The two columns from the definition above
SELECT Col1, Col2
FROM TableName
)