<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://dbshelp.devio.at/index.php?action=history&amp;feed=atom&amp;title=Dbo.uspGetWhereUsedProductIDprocedure</id>
		<title>Dbo.uspGetWhereUsedProductIDprocedure - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://dbshelp.devio.at/index.php?action=history&amp;feed=atom&amp;title=Dbo.uspGetWhereUsedProductIDprocedure"/>
		<link rel="alternate" type="text/html" href="http://dbshelp.devio.at/index.php?title=Dbo.uspGetWhereUsedProductIDprocedure&amp;action=history"/>
		<updated>2026-05-02T22:48:29Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://dbshelp.devio.at/index.php?title=Dbo.uspGetWhereUsedProductIDprocedure&amp;diff=2560&amp;oldid=prev</id>
		<title>Bot: New page: == wikibot ==  {| border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;border-collapse:collapse&quot; |- valign=&quot;top&quot; | '''Procedure | dbo.uspGetWhereUsedProductID |- valign=&quot;top&quot; | '''Description...</title>
		<link rel="alternate" type="text/html" href="http://dbshelp.devio.at/index.php?title=Dbo.uspGetWhereUsedProductIDprocedure&amp;diff=2560&amp;oldid=prev"/>
				<updated>2010-06-23T22:07:52Z</updated>
		
		<summary type="html">&lt;p&gt;New page: == wikibot ==  {| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; style=&amp;quot;border-collapse:collapse&amp;quot; |- valign=&amp;quot;top&amp;quot; | &amp;#039;&amp;#039;&amp;#039;Procedure | dbo.uspGetWhereUsedProductID |- valign=&amp;quot;top&amp;quot; | &amp;#039;&amp;#039;&amp;#039;Description...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== wikibot ==&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; style=&amp;quot;border-collapse:collapse&amp;quot;&lt;br /&gt;
|- valign=&amp;quot;top&amp;quot;&lt;br /&gt;
| '''Procedure&lt;br /&gt;
| dbo.uspGetWhereUsedProductID&lt;br /&gt;
|- valign=&amp;quot;top&amp;quot;&lt;br /&gt;
| '''Description&lt;br /&gt;
| Stored procedure using a recursive query to return all components or assemblies that directly or indirectly use the specified ProductID.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Source ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CREATE PROCEDURE [dbo].[uspGetWhereUsedProductID]&lt;br /&gt;
    @StartProductID [int],&lt;br /&gt;
    @CheckDate [datetime]&lt;br /&gt;
AS&lt;br /&gt;
BEGIN&lt;br /&gt;
    SET NOCOUNT ON;&lt;br /&gt;
&lt;br /&gt;
    --Use recursive query to generate a multi-level Bill of Material (i.e. all level 1 components of a level 0 assembly, all level 2 components of a level 1 assembly)&lt;br /&gt;
    WITH [BOM_cte]([ProductAssemblyID], [ComponentID], [ComponentDesc], [PerAssemblyQty], [StandardCost], [ListPrice], [BOMLevel], [RecursionLevel]) -- CTE name and columns&lt;br /&gt;
    AS (&lt;br /&gt;
        SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], 0 -- Get the initial list of components for the bike assembly&lt;br /&gt;
        FROM [Production].[BillOfMaterials] b&lt;br /&gt;
            INNER JOIN [Production].[Product] p &lt;br /&gt;
            ON b.[ProductAssemblyID] = p.[ProductID] &lt;br /&gt;
        WHERE b.[ComponentID] = @StartProductID &lt;br /&gt;
            AND @CheckDate &amp;gt;= b.[StartDate] &lt;br /&gt;
            AND @CheckDate &amp;lt;= ISNULL(b.[EndDate], @CheckDate)&lt;br /&gt;
        UNION ALL&lt;br /&gt;
        SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], [RecursionLevel] + 1 -- Join recursive member to anchor&lt;br /&gt;
        FROM [BOM_cte] cte&lt;br /&gt;
            INNER JOIN [Production].[BillOfMaterials] b &lt;br /&gt;
            ON cte.[ProductAssemblyID] = b.[ComponentID]&lt;br /&gt;
            INNER JOIN [Production].[Product] p &lt;br /&gt;
            ON b.[ProductAssemblyID] = p.[ProductID] &lt;br /&gt;
        WHERE @CheckDate &amp;gt;= b.[StartDate] &lt;br /&gt;
            AND @CheckDate &amp;lt;= ISNULL(b.[EndDate], @CheckDate)&lt;br /&gt;
        )&lt;br /&gt;
    -- Outer select from the CTE&lt;br /&gt;
    SELECT b.[ProductAssemblyID], b.[ComponentID], b.[ComponentDesc], SUM(b.[PerAssemblyQty]) AS [TotalQuantity] , b.[StandardCost], b.[ListPrice], b.[BOMLevel], b.[RecursionLevel]&lt;br /&gt;
    FROM [BOM_cte] b&lt;br /&gt;
    GROUP BY b.[ComponentID], b.[ComponentDesc], b.[ProductAssemblyID], b.[BOMLevel], b.[RecursionLevel], b.[StandardCost], b.[ListPrice]&lt;br /&gt;
    ORDER BY b.[BOMLevel], b.[ProductAssemblyID], b.[ComponentID]&lt;br /&gt;
    OPTION (MAXRECURSION 25) &lt;br /&gt;
END;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== References ===&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; style=&amp;quot;border-collapse:collapse&amp;quot;&lt;br /&gt;
|- style=&amp;quot;background:silver&amp;quot;&lt;br /&gt;
| '''Dependency Type&lt;br /&gt;
| '''Object Type&lt;br /&gt;
| '''Referenced Object&lt;br /&gt;
&lt;br /&gt;
|- valign=&amp;quot;top&amp;quot;&lt;br /&gt;
| Select&lt;br /&gt;
| Table&lt;br /&gt;
| [[Production.BillOfMaterials_(table)|Production.BillOfMaterials]]&lt;br /&gt;
&lt;br /&gt;
|- valign=&amp;quot;top&amp;quot;&lt;br /&gt;
| Select&lt;br /&gt;
| Table&lt;br /&gt;
| [[Production.Product_(table)|Production.Product]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Bot</name></author>	</entry>

	</feed>