?
Solved

sql server for loop

Posted on 2010-01-04
10
Medium Priority
?
228 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:rgb192
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 26176946
hmmm...are you wantin a list of all possible values from the tables in your new table?  if so, you can do this (without a loop)

select zip, title, description
from zip, products, accessory
where orderid= x and productid = x and accessoryid= x
0
 
LVL 39

Expert Comment

by:appari
ID: 26176952
can you give us more details?
table structures with sample data and what you want to do with the data?
0
 

Author Comment

by:rgb192
ID: 26176982
>>select zip, title, description
from zip, products, accessory
where orderid= x and productid = x and accessoryid= x


but x would be orderid
and orderid goes from 1 to 1000
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 60

Expert Comment

by:chapmandew
ID: 26176985
THEN DESCRIBE YOUR PROBLEM BETTER.
0
 
LVL 39

Expert Comment

by:appari
ID: 26177013
can you provide us with the table structures along with the foreign key columns or relations between the tables marked?
0
 

Author Comment

by:rgb192
ID: 26177088
>>can you provide us with the table structures along with the foreign key columns or relations between the tables marked?

not sure
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 26177091
Tim out.
0
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26177188
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


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

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 26177404
Can you provide sample set and expected result?
0
 

Author Closing Comment

by:rgb192
ID: 31672731
thanks
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question