SQL Server intermediar
Ginger Grant
Instructor
Variabilele sunt necesare pentru a stoca valori
DECLARE @variablename data_type
VARCHAR(n): câmp text cu lungime variabilăINT: valori întregi de la -2.147.483.647 la +2.147.483.647DECIMAL(p ,s) sau NUMERIC(p ,s):p: numărul total de cifre zecimale stocate, atât la stânga, cât și la dreapta punctului zecimals: numărul de cifre zecimale stocate la dreapta punctului zecimal-- Declare Snack as a VARCHAR with length 10
DECLARE @Snack VARCHAR(10)
-- Declare the variable
DECLARE @Snack VARCHAR(10)
-- Use SET a value to the variable
SET @Snack = 'Cookies'
-- Show the value
SELECT @Snack
+--------------------+
|(No column name) |
+--------------------+
|Cookies |
+--------------------+
-- Declare the variable
DECLARE @Snack VARCHAR(10)
-- Use SELECT assign a value
SELECT @Snack = 'Candy'
-- Show the value
SELECT @Snack
+--------------------+
|(No column name) |
+--------------------+
|Candy |
+--------------------+
WHILE evaluează o condiție adevărată sau falsă
După WHILE, urmează o linie cu cuvântul cheie BEGIN
Includeți codul care rulează până când condiția din bucla WHILE devine adevărată
După cod, adăugați cuvântul cheie END
BREAK determină ieșirea din buclă
CONTINUE determină continuarea buclei
-- Declare ctr as an integer DECLARE @ctr INT -- Assign 1 to ctr SET @ctr = 1-- Specify the condition of the WHILE loop WHILE @ctr < 10-- Begin the code to execute inside WHILE loop BEGIN -- Keep incrementing the value of @ctr SET @ctr = @ctr + 1 -- End WHILE loop END -- View the value after the loop SELECT @ctr
+--------------------+
|(No column name) |
+--------------------+
|10 |
+--------------------+
-- Declare ctr as an integer
DECLARE @ctr INT
-- Assign 1 to ctr
SET @ctr = 1
-- Specify the condition of the WHILE loop
WHILE @ctr < 10
-- Begin the code to execute inside WHILE loop
BEGIN
-- Keep incrementing the value of @ctr
SET @ctr = @ctr + 1
-- Check if ctr is equal to 4
IF @ctr = 4
-- When ctr is equal to 4, the loop will break
BREAK
-- End WHILE loop
END
SQL Server intermediar