sql insert variable

would like for x to start at 1 and go to 5


this works

declare @x int
set @x = 1

INSERT INTO information
SELECT
(SELECT productid from products where productid = @x ),
(select accessoryorder from accessorytypes where accessoryid  = @x ),
(SELECT orderid from orders where orderid = @x )
select * from information
LVL 1
rgb192Asked:
Who is Participating?
 
Aneesh RetnakaranDatabase AdministratorCommented:
declare @x int
set @x = 1

while @x < 5
BEGIN

INSERT INTO information
SELECT
(SELECT productid from products where productid = @x ),
(select accessoryorder from accessorytypes where accessoryid  = @x ),
(SELECT orderid from orders where orderid = @x )
SET @x = @x +1
END

select * from information
0
 
chapmandewCommented:
declare @x int
set @x = 1

while @x < 5
BEGIN

INSERT INTO information
SELECT productid from products p
join accessorytypes a on p.productid = a.accessoryid
join orders o on a.accessoryid = o.orderid
where productid = @x

END
0
 
chapmandewCommented:
declare @x int
set @x = 1

while @x < 5
BEGIN

INSERT INTO information
SELECT productid from products p
join accessorytypes a on p.productid = a.accessoryid
join orders o on a.accessoryid = o.orderid
where productid = @x
set @x =  @x + 1
END
0
 
chapmandewCommented:
declare @x int
set @x = 1

while @x < 5
BEGIN

INSERT INTO information
SELECT productid, accessoryorder , orderid from products p
join accessorytypes a on p.productid = a.accessoryid
join orders o on a.accessoryid = o.orderid
where productid = @x
set @x =  @x + 1
END
0
 
rgb192Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.