Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Avoiding loop to get a resultset

Posted on 2007-03-22
3
Medium Priority
?
226 Views
Last Modified: 2013-11-30
Let me explain the scenario (SQL 2000 DTS):
I've a order table and order_detail table.
Order_detail table refers Order table for order_id. Since one order can have multiple items within it we've the order_detail table. So Order->order_detail is a one->many relationship.
Now I want to export the data into a flat file. the format is something like this:

order1_id, order number, date
Order_detail_id1,item_name1
Order_detail_id2, item_name2
order2_id, order number, date
order_detail_id3,item_name3
order_detail_id4, item_name4

So I need to write one line from the order table and then follows the corresponding order_detail table records. Again need to write the order table record and so on. Is there way to do this other looping? I knw we can use a while loop or cursor, I want to know if therz a better way of writing this in t-SQL only avoiding loop.

regards
0
Comment
Question by:rsrsm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
Ved Prakash Agrawal earned 1000 total points
ID: 18770451
can you used vbscript in you dts package and in which you write you loops.
i don't think so without looping we can achive the same things.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 1000 total points
ID: 18770463
you could write a select statement to do it

but a loop is probably best....

(you may also wish to experiment with an xml output?)

e.g.

Select case Linetype when  0 then convert(char(10),o.orderno)+'...'
                   else convert(....
                   end
         +...

from Order as O
Inner join OrderLine as OL
 on o.orderno=ol.orderno
cross join (select 0 as linetype union select 1) as L
Where (ol.sequence = 1 and l.linetype=0)
   or (l.linetype=1)
0
 

Author Comment

by:rsrsm
ID: 18770503
Thank you both since both the answers were quick and almost similar I've divided the points. Please let me know if there are any concerns.
0

Featured Post

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.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

604 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