Question : sql server for loop

INSERT INTO newTable
SELECT
(SELECT zip from orders where orderid = x ) as Zip,
(SELECT title from products where productid = x ) as Title,
(select description from accessory where accessoryid  = x ) as description

could this be put into a for loop and all the values of the table populated

Answer : sql server for loop

create a table say numbers (n: integer)
put values 0,1,2,3,4,5,6,7,8,9

create a view as

create view num1000 as
select n1.n*100+n2.n*10+n3.n + 1 as r
from numbers n1, numbers n2, numbers n3

this view will give you numbers from 1 to 1000

then use this

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
INSERT INTO newTable 
SELECT n.r, o.zip, p.title, a.description
  from num1000 n, orders o, products p, accessory a
 where o.orderid=n.r and p.productid=n.r and a.accessoryid=n.r

if you dont have id column in newtable, just drop "n.r," from above query as:

INSERT INTO newTable 
SELECT o.zip, p.title, a.description
  from num1000 n, orders o, products p, accessory a
 where o.orderid=n.r and p.productid=n.r and a.accessoryid=n.r
Random Solutions  
 
programming4us programming4us