all groups > sql server programming > may 2006 >
You're in the

sql server programming

group:

How to write this query?



How to write this query? VJ
5/31/2006 8:43:28 PM
sql server programming: I've a Bill of Material table that looks some what like this:

Table BOM:
Part# Component#
1 A
1 B
1 C
A X
A Y
B M
B N
B O
B P
B Q
B R
R S
R T
X Z

Column 1 is the part number and Column 2 is the component number.

As you can see, part number 1, is made out of A, B, and C.

But then again part A is made out of X and Y.

And B is made out of M, N, O, P, Q, R.

R is made out of S and T.

X is made out of Z.

That means C, M, N, O, P, Q, S, Y and Z are purchase parts. They are
bought from the suppliers directly. Others are made out of these
purchase parts.

I have another table that holds the cost for all the purchase parts and
all the manufactured parts. How do I write a stored procedure so that
if I supply the part number, it gets me the total cost for that part?
There can be one component part OR several. There is no limit on how
many components you can have on a part. If I want to user Cursor and
While loop, how do I loop thru each part to get the cost, add up and
keep going until I get the total cost for the parent part? Any input
will be highly appreciated.

Thanks in advance..
Re: How to write this query? Razvan Socol
5/31/2006 10:32:26 PM
Hello, VJ

If you are using SQL 2005, read the following article by Itzik Ben-Gan,
especially the part about recursive CTE-s (see Figure 8 in the
article):
http://msdn.microsoft.com/msdnmag/issues/04/02/TSQLinYukon/

Razvan
Re: How to write this query? VJ
6/1/2006 1:24:23 PM
Hi Razvan -

Thanks for the input. Unfortunately, we are not SQL 2005 yet. Though, I
was able to figure out the solution using Cursor, While loop and the
temp tables. It is working fine - though after researching a little bit
- I came across some Extended Stored Procs such as sp_OACreate etc.
that allows to create an object and then properties can be assigned to
this object. I am hoping to get this done using these extended stored
procs if possible. Anyone, with experience with these procs, please let
me know. Not sure, how this impacts the performance either. I will have
to do a little research to figure this out - I can post the code, if
anyone is interested.

Thanks..


[quoted text, click to view]
Re: How to write this query? Razvan Socol
6/1/2006 9:44:20 PM
Hi, VJ

I believe a solution using temp tables and a while loop (without
cursors) would be possible and more efficient (if all rows for a
particular level are processed at the same time). Also, if you don't
allow more than 30 levels of nesting for the components, you can use
recursive UDF-s (which seem more elegant and possibly have the same
performance).

I would not recommend using sp_OACreate/etc to call some object
repetitively (for each row in a table), because the overhead may be
significant (and even the impact on the stability of the server should
be considered). I think it's better to do the manipulations in SQL if
there is more data. I would use sp_OACreate only if the computations
are very CPU-intensive but require a small amount of data, or if you
really must use some features that are not available in SQL Server.

Razvan
AddThis Social Bookmark Button