简体   繁体   English

select 的存储过程逻辑

[英]Stored procedure logic for select

I have the following procedure:我有以下程序:

CREATE PROCEDURE sp_types
    @type varchar(100)
AS
BEGIN
    DECLARE @products table(productId int)

    IF @type = 'Merchandise'
    BEGIN
        INSERT INTO @products
            SELECT productId
            FROM dbo.product
            WHERE type = @type
    END
    ELSE IF @type = 'Electronics'
    BEGIN
        INSERT INTO @products
            SELECT productId
            FROM dbo.product
            WHERE type = @type
    END
    ELSE IF @type = 'Home'
    BEGIN
        INSERT INTO @products
            SELECT productId
            FROM dbo.product
            WHERE type = @type
    END
    ELSE
    BEGIN
        INSERT INTO @products
            SELECT productId
            FROM dbo.product
            WHERE type = @type
    END

    /* here we have logic to convert all the productids in the @products table into an XML format
    <products>
      <productId>1</productId>
      <productId>2</productId>
    ....
    ....
    ....
      <productId>100</productId>
    </products>
    */

    /* after we get the XML string, it is passed to another procedure to print out details about the products */
    EXEC sp_products_list @xml = @productXml

END /* procedure ends here */

Here's the sp_products_list procedure:这是sp_products_list过程:

CREATE PROCEDURE sp_products_list
    @xml XML
AS
BEGIN
    DECLARE @products TABLE (productId int)

    INSERT INTO @products
        SELECT @xml.value('productId','int')
        FROM @xml.nodes('products')

    /* Select statement */
    SELECT
        a.productId, a.productName, 
        b.productRegion, b.ProductQuantity, 
        c.productSupplier
    FROM
        products a
    JOIN 
        productRegion b ON a.productid = b.productid
    JOIN 
        productSupplier c ON c.productRegion = b.productRegion
    WHERE 
        a.productId IN (SELECT productId FROM @products)

END /* procedure end */

The sp_products_list is called by many other procedures other than the sp_types procedure. sp_products_list由除sp_types过程之外的许多其他过程调用。 I have a requirement wherein when I pass a type of 'Merchandise' to the sp_types procedure, then I need some additional columns like productSupplierRegion , productSupplierCount etc. displayed.我有一个要求,当我将“商品”类型传递给sp_types过程时,我需要显示一些额外的列,如productSupplierRegionproductSupplierCount等。

But for the rest of the types, I only need to display what the select statement in the sp_products_list procedure currently displays.但是对于 rest 类型,我只需要显示sp_products_list过程中的 select 语句当前显示的内容。

If I simply add the columns that I need to the select statement in the current sp_products_list procedure then they will be displayed for any type that is passed to the sp_types procedures and that is not what I want.如果我只是将我需要的列添加到当前sp_products_list过程中的 select 语句中,那么它们将针对传递给sp_types过程的任何类型显示,这不是我想要的。

My solution: one of the solutions I could think of was receiving a @type variable as an input in the sp_products_list procedure and having an if-else statement for the select statement.我的解决方案:我能想到的解决方案之一是在sp_products_list过程中接收一个 @type 变量作为输入,并为 select 语句设置一个 if-else 语句。 If the type of 'Merchandise' is passed in then display the select with additional columns otherwise display the regular columns.如果传入“商品”的类型,则显示 select 和其他列,否则显示常规列。

The problem I might face in the future with this approach is what if we want to add different types of columns for different @type variables that are passed in. In that case, I'll have to have to do multiple if-else statements for each type.我将来使用这种方法可能面临的问题是,如果我们想为传入的不同@type变量添加不同类型的列怎么办。在这种情况下,我将不得不执行多个 if-else 语句每种类型。 I was planning on using dynamic SQL but my idea was shot down since my team is not a huge fan of dynamic SQL.我计划使用动态 SQL 但我的想法被否决了,因为我的团队不是动态 SQL 的忠实粉丝。

Now I'm trying to find a robust solution for this problem that might work in any scenario.现在,我正在尝试为这个问题找到一个可能在任何情况下都有效的强大解决方案。 Any thoughts or suggestions?有什么想法或建议吗? Thank you!谢谢!

I suggest you rewrite sp_products_list as a table-valued function, then you can just join it with any extra columns you need我建议您将sp_products_list重写为表值 function,然后您可以将它与您需要的任何额外列连接起来

CREATE OR ALTER FUNCTION dbo.products_list ( @productId int )
RETURNS TABLE    
AS RETURN

    SELECT
        p.productId, p.productName, 
        pr.productRegion, pr.ProductQuantity, 
        ps.productSupplier
    FROM
        products p
    JOIN 
        productRegion pr ON p.productid = pr.productid
    JOIN 
        productSupplier ps ON ps.productRegion = pr.productRegion
    WHERE 
        p.productId = @productId;

GO

Then you simply use that function in each of the procedures.然后,您只需在每个程序中使用该 function 即可。 For example.例如。

CREATE PROCEDURE ProductByType
    @type varchar(100)
AS

    IF @type = 'Merchandise'
    BEGIN
        SELECT pl.*, p.OtherColumns, t.OtherTablesColumns
            FROM dbo.product p
            CROSS APPLY dbo.products_list (p.productId) pl
            LEFT JOIN OtherTable t ON SomeCondition
            WHERE p.type = @type;
    END;
    ELSE
    BEGIN
        SELECT pl.*
            FROM dbo.product p
            CROSS APPLY dbo.products_list (p.productId) pl
            WHERE p.type = @type;
    END;

You may find it more performant to push the @type selection into the function also.您可能会发现将@type 选择推@type中会更高效。 Then you can simply do然后你可以简单地做

SELECT pl.*
FROM dbo.products_list_by_type (@type) pl

The server will remove any columns that are not needed from the query plan.服务器将从查询计划中删除任何不需要的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM