While with counter : while « Transact SQL « SQL Server / T-SQL Tutorial






8> CREATE TABLE Product(
9>     ProductID               int                NOT NULL,
10>     Name                    nvarchar(25)       NOT NULL,
11>      ProductNumber           nvarchar(25)               ,
12>      Color                   nvarchar(15)       NULL,
13>      StandardCost            money              NOT NULL,
14>      Size                    nvarchar(5)        NULL,
15>      Weight                  decimal(8, 2)      NULL,
16>      ProductLine             nchar(20)           NULL,
17>      SellStartDate           datetime           NOT NULL,
18>      SellEndDate             datetime           NULL
19>  )
20>  GO
1> insert into Product values(1,'Product A', '1','Red',123.123,'1',1,'ProductLine A','1999-03-22','2000-03-22');
2> GO

(1 rows affected)
1>
2>
3> insert into Product values(2,'Product B', '2','Yellow',234.234,'1',3,'ProductLine B','2000-03-22','2001-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(3,'Product C', '3','Pink',345.345,'1',3,'ProductLine V','2001-09-22','2006-02-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(4,'Product D', '4','White',456.456,'1',4,'ProductLine D','2002-08-22','2006-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(5,'Product E', '5','Black',567.567,'1',5,'ProductLine E','2003-01-22','2003-04-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(6,'Product F', '6','Blue',678.678,'1',6,'ProductLine W','2004-02-22','2005-05-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(7,'Product G', '7','Drak',789.789,'1',7,'ProductLine Q','2005-03-22','2006-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(8,'Product H', '8','Gray',234.123,'1',8,'ProductLine F','2006-04-22','2006-09-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(9,'Product I', '9','Red',543.123,'1',9,'ProductLine R','2007-05-22','2008-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(0,'Product J', '0','Gold',765.123,'1',0,'ProductLine J','2008-06-22','2009-03-22');
4> GO

(1 rows affected)
1>
2>
3>
4>
5>
6>
7> DECLARE @Counter Int
8> SET @Counter = 1
9> WHILE @Counter < 4
10>  BEGIN
11>   PRINT 'SubCategory '
12>   SELECT Name, ProductID
13>   FROM Product
14>   WHERE ProductID = @Counter
15>    SET @Counter = @Counter + 1
16>  END
17>  GO
SubCategory
Name                      ProductID
------------------------- -----------
Product A                           1

(1 rows affected)
SubCategory
Name                      ProductID
------------------------- -----------
Product B                           2

(1 rows affected)
SubCategory
Name                      ProductID
------------------------- -----------
Product C                           3

(1 rows affected)
1>
2>
3> drop table Product;
4> GO








20.8.while
20.8.1.How to perform repetitive processing
20.8.2.WHILE @@FETCH_STATUS = 0
20.8.3.This procedure inserts rows by using while loop
20.8.4.Insert 100 rows of data: RAND
20.8.5.While with counter
20.8.6.Use while loop to insert data
20.8.7.While loop controlled by an aggregate function
20.8.8.WHILE with AND operator
20.8.9.A script that tests and adjusts credit amounts with a WHILE loop
20.8.10.Using a Subquery with a Single-Statement WHILE Loop