중급 SQL Server
Ginger Grant
Instructor
값을 설정하려면 변수가 필요합니다
DECLARE @variablename data_type
VARCHAR(n): 가변 길이 텍스트 필드INT: -2,147,483,647 ~ +2,147,483,647 범위의 정수DECIMAL(p ,s) 또는 NUMERIC(p ,s):p: 소수점 왼쪽과 오른쪽을 포함한 전체 자릿수s: 소수점 오른쪽에 저장할 자릿수-- 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는 참 또는 거짓 조건을 평가합니다
WHILE 다음 줄에는 BEGIN 키워드를 작성합니다
WHILE 루프 조건이 참이 될 때까지 실행할 코드를 작성합니다
코드 뒤에 END 키워드를 추가합니다
BREAK는 루프를 종료합니다
CONTINUE는 루프를 계속 실행합니다
-- 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