• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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
0
rgb192
Asked:
rgb192
  • 3
1 Solution
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now