How to Work With WHILE Statements in Transact-SQL

In this article I am going to explain how to work with while statements in T SQL.
  • 2083

Introduction

Transact-SQL (T-SQL) is Structured Query Language and it is Microsoft's and Sybase's proprietary. Transact-SQL is originally developed by IBM for querying, altering and defining relational database. Transact SQL is also known as T-SQL. T-SQL supports many feature like variables, loops, conditions  and exceptions.   

WHILE Statement

While statements execute a sequence of statements multiple times. Statements in the while body are executed as long as a condition is true.

while (condition)
begin
Statement 1
Statement 2
Statement 3
Statement 4
.
.
.
Statement n
end

Example

declare @num int

declare @countr int=1

declare @temp int

declare @result varchar(15)

select @num = 2

while(@countr<=10)

begin

select @temp = @num*@countr

print convert(char(10),@temp)

select @countr = @countr + 1

end


Output

Clipboard01.jpg


© 2019 DotNetHeaven. All rights reserved.