Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
Avatar of rgb192

ASKER

thanks