我需要从SQL Server存储过程中获取列名

时间:2016-06-22 11:18:17

标签: sql-server

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存储过程中使用的所有列名?我需要在子句中使用的列名,例如select,where,group by,order by等。

提前致谢

1 个答案:

答案 0 :(得分:0)

我认为没有任何查询能够准确地为您提供存储过程中使用的所有列。有一种方法可以使用SSMS来完成它。

  • 转到对象资源管理器&gt;数据库&gt; YourDatabase&gt;可编程性&gt;存储过程
  • 右键单击YourProcedure&gt;查看依赖关系
  • 选择单选按钮列表中的第二个选项([YourProcedure]所依赖的对象

您将获得程序所依赖的所有表,表链接和列的树视图。

如果这是你想要的,请告诉我。