Use while loop to insert data : While « Transact SQL « SQL Server / T-SQL

Home
SQL Server / T-SQL
1.Aggregate Functions
2.Analytical Functions
3.Constraints
4.Cursor
5.Data Set
6.Data Type
7.Database
8.Date Timezone
9.Index
10.Insert Delete Update
11.Math Functions
12.Select Query
13.Sequence
14.Store Procedure Function
15.String Functions
16.Subquery
17.System
18.Table
19.Table Joins
20.Transact SQL
21.Transaction
22.Trigger
23.View
24.XML
SQL Server / T-SQL » Transact SQL » While 
Use while loop to insert data

 


6> IF OBJECT_ID('dbo.Nums') IS NOT NULL
7>   DROP TABLE dbo.Nums;
8> GO
1CREATE TABLE dbo.Nums(n INT NOT NULL PRIMARY KEY);
2> DECLARE @max AS INT, @rc AS INT;
3> SET @max = 1000000;
4> SET @rc = 1;
5>
6INSERT INTO Nums VALUES(1);
7> WHILE @rc * <= @max
8BEGIN
9>   INSERT INTO dbo.Nums SELECT n + @rc FROM dbo.Nums;
10>   SET @rc = @rc * 2;
11END
12>
13INSERT INTO dbo.Nums
14>   SELECT n + @rc FROM dbo.Nums WHERE n + @rc <= @max;
15>
16> DECLARE @s AS DATETIME, @e AS DATETIME;
17> SET @s = '20060101';
18> SET @e = '20061231';
19>
20SELECT @s + n - AS dt
21FROM dbo.Nums
22WHERE n <= DATEDIFF(day, @s, @e1;
23> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(16 rows affected)

(32 rows affected)

(64 rows affected)

(128 rows affected)

(256 rows affected)

(512 rows affected)

(1024 rows affected)

(2048 rows affected)

(4096 rows affected)

(8192 rows affected)

(16384 rows affected)

(32768 rows affected)

(65536 rows affected)

(131072 rows affected)

(262144 rows affected)

(475712 rows affected)
dt
-----------------------
2006-01-01 00:00:00.000
2006-01-02 00:00:00.000
2006-01-03 00:00:00.000
2006-01-04 00:00:00.000
2006-01-05 00:00:00.000
2006-01-06 00:00:00.000
2006-01-07 00:00:00.000
2006-01-08 00:00:00.000
2006-01-09 00:00:00.000
2006-01-10 00:00:00.000
2006-01-11 00:00:00.000
2006-01-12 00:00:00.000
2006-01-13 00:00:00.000
2006-01-14 00:00:00.000
2006-01-15 00:00:00.000
2006-01-16 00:00:00.000
2006-01-17 00:00:00.000
2006-01-18 00:00:00.000
2006-01-19 00:00:00.000
2006-01-20 00:00:00.000
2006-01-21 00:00:00.000
2006-01-22 00:00:00.000
2006-01-23 00:00:00.000
2006-01-24 00:00:00.000
2006-01-25 00:00:00.000
2006-01-26 00:00:00.000
2006-01-27 00:00:00.000
2006-01-28 00:00:00.000
2006-01-29 00:00:00.000
2006-01-30 00:00:00.000
2006-01-31 00:00:00.000
2006-02-01 00:00:00.000
2006-02-02 00:00:00.000
2006-02-03 00:00:00.000
2006-02-04 00:00:00.000
2006-02-05 00:00:00.000
2006-02-06 00:00:00.000
2006-02-07 00:00:00.000
2006-02-08 00:00:00.000
2006-02-09 00:00:00.000
2006-02-10 00:00:00.000
2006-02-11 00:00:00.000
2006-02-12 00:00:00.000
2006-02-13 00:00:00.000
2006-02-14 00:00:00.000
2006-02-15 00:00:00.000
2006-02-16 00:00:00.000
2006-02-17 00:00:00.000
2006-02-18 00:00:00.000
2006-02-19 00:00:00.000
2006-02-20 00:00:00.000
2006-02-21 00:00:00.000
2006-02-22 00:00:00.000
2006-02-23 00:00:00.000
2006-02-24 00:00:00.000
2006-02-25 00:00:00.000
2006-02-26 00:00:00.000
2006-02-27 00:00:00.000
2006-02-28 00:00:00.000
2006-03-01 00:00:00.000
2006-03-02 00:00:00.000
2006-03-03 00:00:00.000
2006-03-04 00:00:00.000
2006-03-05 00:00:00.000
2006-03-06 00:00:00.000
2006-03-07 00:00:00.000
2006-03-08 00:00:00.000
2006-03-09 00:00:00.000
2006-03-10 00:00:00.000
2006-03-11 00:00:00.000
2006-03-12 00:00:00.000
2006-03-13 00:00:00.000
2006-03-14 00:00:00.000
2006-03-15 00:00:00.000
2006-03-16 00:00:00.000
2006-03-17 00:00:00.000
2006-03-18 00:00:00.000
2006-03-19 00:00:00.000
2006-03-20 00:00:00.000
2006-03-21 00:00:00.000
2006-03-22 00:00:00.000
2006-03-23 00:00:00.000
2006-03-24 00:00:00.000
2006-03-25 00:00:00.000
2006-03-26 00:00:00.000
2006-03-27 00:00:00.000
2006-03-28 00:00:00.000
2006-03-29 00:00:00.000
2006-03-30 00:00:00.000
2006-03-31 00:00:00.000
2006-04-01 00:00:00.000
2006-04-02 00:00:00.000
2006-04-03 00:00:00.000
2006-04-04 00:00:00.000
2006-04-05 00:00:00.000
2006-04-06 00:00:00.000
2006-04-07 00:00:00.000
2006-04-08 00:00:00.000
2006-04-09 00:00:00.000
2006-04-10 00:00:00.000
2006-04-11 00:00:00.000
2006-04-12 00:00:00.000
2006-04-13 00:00:00.000
2006-04-14 00:00:00.000
2006-04-15 00:00:00.000
2006-04-16 00:00:00.000
2006-04-17 00:00:00.000
2006-04-18 00:00:00.000
2006-04-19 00:00:00.000
2006-04-20 00:00:00.000
2006-04-21 00:00:00.000
2006-04-22 00:00:00.000
2006-04-23 00:00:00.000
2006-04-24 00:00:00.000
2006-04-25 00:00:00.000
2006-04-26 00:00:00.000
2006-04-27 00:00:00.000
2006-04-28 00:00:00.000
2006-04-29 00:00:00.000
2006-04-30 00:00:00.000
2006-05-01 00:00:00.000
2006-05-02 00:00:00.000
2006-05-03 00:00:00.000
2006-05-04 00:00:00.000
2006-05-05 00:00:00.000
2006-05-06 00:00:00.000
2006-05-07 00:00:00.000
2006-05-08 00:00:00.000
2006-05-09 00:00:00.000
2006-05-10 00:00:00.000
2006-05-11 00:00:00.000
2006-05-12 00:00:00.000
2006-05-13 00:00:00.000
2006-05-14 00:00:00.000
2006-05-15 00:00:00.000
2006-05-16 00:00:00.000
2006-05-17 00:00:00.000
2006-05-18 00:00:00.000
2006-05-19 00:00:00.000
2006-05-20 00:00:00.000
2006-05-21 00:00:00.000
2006-05-22 00:00:00.000
2006-05-23 00:00:00.000
2006-05-24 00:00:00.000
2006-05-25 00:00:00.000
2006-05-26 00:00:00.000
2006-05-27 00:00:00.000
2006-05-28 00:00:00.000
2006-05-29 00:00:00.000
2006-05-30 00:00:00.000
2006-05-31 00:00:00.000
2006-06-01 00:00:00.000
2006-06-02 00:00:00.000
2006-06-03 00:00:00.000
2006-06-04 00:00:00.000
2006-06-05 00:00:00.000
2006-06-06 00:00:00.000
2006-06-07 00:00:00.000
2006-06-08 00:00:00.000
2006-06-09 00:00:00.000
2006-06-10 00:00:00.000
2006-06-11 00:00:00.000
2006-06-12 00:00:00.000
2006-06-13 00:00:00.000
2006-06-14 00:00:00.000
2006-06-15 00:00:00.000
2006-06-16 00:00:00.000
2006-06-17 00:00:00.000
2006-06-18 00:00:00.000
2006-06-19 00:00:00.000
2006-06-20 00:00:00.000
2006-06-21 00:00:00.000
2006-06-22 00:00:00.000
2006-06-23 00:00:00.000
2006-06-24 00:00:00.000
2006-06-25 00:00:00.000
2006-06-26 00:00:00.000
2006-06-27 00:00:00.000
2006-06-28 00:00:00.000
2006-06-29 00:00:00.000
2006-06-30 00:00:00.000
2006-07-01 00:00:00.000
2006-07-02 00:00:00.000
2006-07-03 00:00:00.000
2006-07-04 00:00:00.000
2006-07-05 00:00:00.000
2006-07-06 00:00:00.000
2006-07-07 00:00:00.000
2006-07-08 00:00:00.000
2006-07-09 00:00:00.000
2006-07-10 00:00:00.000
2006-07-11 00:00:00.000
2006-07-12 00:00:00.000
2006-07-13 00:00:00.000
2006-07-14 00:00:00.000
2006-07-15 00:00:00.000
2006-07-16 00:00:00.000
2006-07-17 00:00:00.000
2006-07-18 00:00:00.000
2006-07-19 00:00:00.000
2006-07-20 00:00:00.000
2006-07-21 00:00:00.000
2006-07-22 00:00:00.000
2006-07-23 00:00:00.000
2006-07-24 00:00:00.000
2006-07-25 00:00:00.000
2006-07-26 00:00:00.000
2006-07-27 00:00:00.000
2006-07-28 00:00:00.000
2006-07-29 00:00:00.000
2006-07-30 00:00:00.000
2006-07-31 00:00:00.000
2006-08-01 00:00:00.000
2006-08-02 00:00:00.000
2006-08-03 00:00:00.000
2006-08-04 00:00:00.000
2006-08-05 00:00:00.000
2006-08-06 00:00:00.000
2006-08-07 00:00:00.000
2006-08-08 00:00:00.000
2006-08-09 00:00:00.000
2006-08-10 00:00:00.000
2006-08-11 00:00:00.000
2006-08-12 00:00:00.000
2006-08-13 00:00:00.000
2006-08-14 00:00:00.000
2006-08-15 00:00:00.000
2006-08-16 00:00:00.000
2006-08-17 00:00:00.000
2006-08-18 00:00:00.000
2006-08-19 00:00:00.000
2006-08-20 00:00:00.000
2006-08-21 00:00:00.000
2006-08-22 00:00:00.000
2006-08-23 00:00:00.000
2006-08-24 00:00:00.000
2006-08-25 00:00:00.000
2006-08-26 00:00:00.000
2006-08-27 00:00:00.000
2006-08-28 00:00:00.000
2006-08-29 00:00:00.000
2006-08-30 00:00:00.000
2006-08-31 00:00:00.000
2006-09-01 00:00:00.000
2006-09-02 00:00:00.000
2006-09-03 00:00:00.000
2006-09-04 00:00:00.000
2006-09-05 00:00:00.000
2006-09-06 00:00:00.000
2006-09-07 00:00:00.000
2006-09-08 00:00:00.000
2006-09-09 00:00:00.000
2006-09-10 00:00:00.000
2006-09-11 00:00:00.000
2006-09-12 00:00:00.000
2006-09-13 00:00:00.000
2006-09-14 00:00:00.000
2006-09-15 00:00:00.000
2006-09-16 00:00:00.000
2006-09-17 00:00:00.000
2006-09-18 00:00:00.000
2006-09-19 00:00:00.000
2006-09-20 00:00:00.000
2006-09-21 00:00:00.000
2006-09-22 00:00:00.000
2006-09-23 00:00:00.000
2006-09-24 00:00:00.000
2006-09-25 00:00:00.000
2006-09-26 00:00:00.000
2006-09-27 00:00:00.000
2006-09-28 00:00:00.000
2006-09-29 00:00:00.000
2006-09-30 00:00:00.000
2006-10-01 00:00:00.000
2006-10-02 00:00:00.000
2006-10-03 00:00:00.000
2006-10-04 00:00:00.000
2006-10-05 00:00:00.000
2006-10-06 00:00:00.000
2006-10-07 00:00:00.000
2006-10-08 00:00:00.000
2006-10-09 00:00:00.000
2006-10-10 00:00:00.000
2006-10-11 00:00:00.000
2006-10-12 00:00:00.000
2006-10-13 00:00:00.000
2006-10-14 00:00:00.000
2006-10-15 00:00:00.000
2006-10-16 00:00:00.000
2006-10-17 00:00:00.000
2006-10-18 00:00:00.000
2006-10-19 00:00:00.000
2006-10-20 00:00:00.000
2006-10-21 00:00:00.000
2006-10-22 00:00:00.000
2006-10-23 00:00:00.000
2006-10-24 00:00:00.000
2006-10-25 00:00:00.000
2006-10-26 00:00:00.000
2006-10-27 00:00:00.000
2006-10-28 00:00:00.000
2006-10-29 00:00:00.000
2006-10-30 00:00:00.000
2006-10-31 00:00:00.000
2006-11-01 00:00:00.000
2006-11-02 00:00:00.000
2006-11-03 00:00:00.000
2006-11-04 00:00:00.000
2006-11-05 00:00:00.000
2006-11-06 00:00:00.000
2006-11-07 00:00:00.000
2006-11-08 00:00:00.000
2006-11-09 00:00:00.000
2006-11-10 00:00:00.000
2006-11-11 00:00:00.000
2006-11-12 00:00:00.000
2006-11-13 00:00:00.000
2006-11-14 00:00:00.000
2006-11-15 00:00:00.000
2006-11-16 00:00:00.000
2006-11-17 00:00:00.000
2006-11-18 00:00:00.000
2006-11-19 00:00:00.000
2006-11-20 00:00:00.000
2006-11-21 00:00:00.000
2006-11-22 00:00:00.000
2006-11-23 00:00:00.000
2006-11-24 00:00:00.000
2006-11-25 00:00:00.000
2006-11-26 00:00:00.000
2006-11-27 00:00:00.000
2006-11-28 00:00:00.000
2006-11-29 00:00:00.000
2006-11-30 00:00:00.000
2006-12-01 00:00:00.000
2006-12-02 00:00:00.000
2006-12-03 00:00:00.000
2006-12-04 00:00:00.000
2006-12-05 00:00:00.000
2006-12-06 00:00:00.000
2006-12-07 00:00:00.000
2006-12-08 00:00:00.000
2006-12-09 00:00:00.000
2006-12-10 00:00:00.000
2006-12-11 00:00:00.000
2006-12-12 00:00:00.000
2006-12-13 00:00:00.000
2006-12-14 00:00:00.000
2006-12-15 00:00:00.000
2006-12-16 00:00:00.000
2006-12-17 00:00:00.000
2006-12-18 00:00:00.000
2006-12-19 00:00:00.000
2006-12-20 00:00:00.000
2006-12-21 00:00:00.000
2006-12-22 00:00:00.000
2006-12-23 00:00:00.000
2006-12-24 00:00:00.000
2006-12-25 00:00:00.000
2006-12-26 00:00:00.000
2006-12-27 00:00:00.000
2006-12-28 00:00:00.000
2006-12-29 00:00:00.000
2006-12-30 00:00:00.000
2006-12-31 00:00:00.000

(365 rows affected)

 
Related examples in the same category
1.Looping with the WHILE Statement
2.While Loop Demo
3.while loop with counter
4.While with aggregate function
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.