Solved

Avoiding loop to get a resultset

Posted on 2007-03-22
3
222 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 250 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 250 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

688 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