Solved

Avoiding loop to get a resultset

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now