StumbleUpon
Share on Facebook

Saturday, September 5, 2009

While Loop in SQL Server

While loop in T-SQL

Here is a simple example of While loop

Declare @temp tinyint = 1

while (@temp < 11)

begin

print cast(@temp as varchar(2)) + ' X 2 = ' + cast(@temp * 2 as char)

set @temp += 1

end

The above code will be having the following output:

Output

1 X 2 = 2

2 X 2 = 4

3 X 2 = 6

4 X 2 = 8

5 X 2 = 10

6 X 2 = 12

7 X 2 = 14

8 X 2 = 16

9 X 2 = 18

10 X 2 = 20

2 comments:

SATHYA said...

how to get output like this and get store in data table in c#

26 May 2002 - 01 Jun 2002
02 Jun 2002 - 08 Jun 2002
09 Jun 2002 - 15 Jun 2002
16 Jun 2002 - 22 Jun 2002
23 Jun 2002 - 29 Jun 2002
30 Jun 2002 - 06 Jul 2002

s.gireesh said...

Hi sathya.You will use convert function in backend..like this

CONVERT(varchar,getdate(),106)

Related Posts Plugin for WordPress, Blogger...