2016-06-22 13 views
0
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[uspGetBillOfMaterials] 
    @StartProductID [int], 
    @CheckDate [datetime] 
AS 
BEGIN 
    SET NOCOUNT ON; 

    -- 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) 
    -- The CheckDate eliminates any components that are no longer used in the product on this date. 
    WITH [BOM_cte]([ProductAssemblyID], [ComponentID], [ComponentDesc], [PerAssemblyQty], [StandardCost], [ListPrice], [BOMLevel], [RecursionLevel]) -- CTE name and columns 
    AS (
     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 
     FROM [Production].[BillOfMaterials] b 
      INNER JOIN [Production].[Product] p 
      ON b.[ComponentID] = p.[ProductID] 
     WHERE b.[ProductAssemblyID] = @StartProductID 
      AND @CheckDate >= b.[StartDate] 
      AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate) 
     UNION ALL 
     SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], [RecursionLevel] + 1 -- Join recursive member to anchor 
     FROM [BOM_cte] cte 
      INNER JOIN [Production].[BillOfMaterials] b 
      ON b.[ProductAssemblyID] = cte.[ComponentID] 
      INNER JOIN [Production].[Product] p 
      ON b.[ComponentID] = p.[ProductID] 
     WHERE @CheckDate >= b.[StartDate] 
      AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate) 
     ) 
    -- Outer select from the CTE 
    SELECT 
     b.[ProductAssemblyID], b.[ComponentID], b.[ComponentDesc], 
     SUM(b.[PerAssemblyQty]) AS [TotalQuantity], b.[StandardCost], 
     b.[ListPrice], b.[BOMLevel], b.[RecursionLevel] 
    FROM 
     [BOM_cte] b 
    GROUP BY 
     b.[ComponentID], b.[ComponentDesc], b.[ProductAssemblyID], 
     b.[BOMLevel], b.[RecursionLevel], b.[StandardCost], b.[ListPrice] 
    ORDER BY 
     b.[BOMLevel], b.[ProductAssemblyID], b.[ComponentID] 
    OPTION (MAXRECURSION 25) 
END; 

SQL Serverストアドプロシージャで使用されるすべての列名を取得する方法はありますか?私は順序など、によってSQL Serverストアドプロシージャから列名を取得する必要があります

おかげで、別にグループが事前

+0

'FMTONLY' https://msdn.microsoft.com/en-US/library/ms173839.aspx –

+0

@IvanStarostin - よくある質問 –

+0

@IvanStarostin - しかしOPは役に立ちません。これにより、列は 'SELECT'リストにのみ表示されます。 OPは 'where、group by、by order byなど 'で使用されるカラムを必要とします –

答えて

0

に私はあなたに正確にすべて与えることができます任意のクエリがあるとは思わない、などの選択などの句で使用する列名を、必要としますストアドプロシージャで使用される列。あなたはSSMSを使ってそれを行うことができる1つの方法があります。

  • ゴーエクスプローラ>データベース> YourDatabase>プログラミング>ストアドプロシージャをオブジェクトへ
  • 右YourProcedureをクリック>表示の依存関係
  • は(ラジオボタンリストで[YourProcedure]
依存しているオブジェクトの2番目のオプションを選択します

プロシージャが依存するすべてのテーブル、テーブルのリンク、および列のツリービューが表示されます。

あなたは欲しかった。

+0

ありがとう、しかし、私はどこにでも私を取るdid'nt上記のカラム名が必要です。 –

+0

正しく試しましたか?この方法では、SQLストアドプロシージャが依存している列の名前がわかります。 –

+0

私はまた、単純な "Select A、B、C from dbo.MyTable"を実行し、A、B、Cではなく "MyTable"のみを表示するストアドプロシージャでこれを試しました。 – granadaCoder

関連する問題