Solved

Avoiding loop to get a resultset

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

827 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