批判这个SQL存储过程

时间:2009-12-23 22:39:55

标签: sql stored-procedures

下面是合同开发人员为我写的一个大规模存储过程,我觉得我正在挑选开发人员,但这很糟糕。您可以看到哪些主要问题?

CREATE PROCEDURE [dbo].[usp_SHS_XXXX]  
( 
    @request_identifier varchar(255),
    @category_guids varchar(4000),
    @url varchar(500),
    @section_id int,
    @NoOfDaysToRank int = 45,
    @SaleRankWeight decimal(18,2) = 1.0,
    @QuantityRankWeight decimal(18,2) = 1.0,
    @NewItemWeight decimal(18,2) = 0.2      
)
AS  
BEGIN  
    SET NOCOUNT ON

    declare @tblCatAttrFilteredPhysicalItems table
    (
        [request_identifier] [uniqueidentifier] NULL,
        [item_guid] [uniqueidentifier] NULL,
        [item_cd] [varchar](25) NULL,
        [master_guid] [uniqueidentifier] NULL,
        [item_type_id] [int] NULL,
        [item_description_title] [varchar](100) NULL,
        [item_description_short] [varchar](255) NULL,
        [item_description] [varchar](3000) NULL,
        [item_retail_price] [decimal](18, 2) NULL,
        [item_sale_price] [decimal](18, 2) NULL,
        [item_backorderable] [int] NULL,
        [item_discontinued] [int] NULL,
        [item_available] [int] NULL,
        [item_catalog_guid] [uniqueidentifier] NULL,
        [item_image_counter] [int] NULL,
        [item_color] [varchar](255) NULL,
        [item_gender] [varchar](255) NULL,
        [item_age_group] [varchar](255) NULL,
        [item_price_updated] [varchar](25) NULL,
        [item_licensor] [varchar](255) NULL,
        [item_manufacturer] [varchar](255) NULL,
        [item_primary_license] [varchar](255) NULL,
        [item_series] [varchar](255) NULL,
        [item_size] [varchar](255) NULL,
        [item_associated_hero] [varchar](255) NULL,
        [item_tshirt_attributes] [varchar](255) NULL,
        [item_date_counted] [varchar](25) NULL,
        [item_hide_from_search] [varchar](255) NULL,
        [item_holiday] [varchar](255) NULL,
        [item_material] [varchar](255) NULL,
        [item_newphoto_needed] [varchar](255) NULL,
        [item_sleeve_type] [varchar](255) NULL,
        [item_softness] [varchar](255) NULL,
        [item_created] [datetime] NULL,
        [item_approved] [int] NULL,
        [item_quantity_on_hand] [int] NULL,
        [item_quantity_on_hold] [int] NULL,
        [item_quantity_on_order] [int] NULL,
        [item_weight] [decimal](18,2) NULL,
        [is_item_new] [varchar](5) NULL,
        [category_guid] [varchar](255) NULL,
        [category_name] [varchar](50) NULL,
        [item_quantity] [int],
        [item_price_adjustment_value] [decimal](16,6),
        [item_control_link] [nvarchar](500),
        [item_promotion] [nvarchar](255),
        [item_price_adjustment] [decimal](16,6)
    )

    declare
        @attribute_cd varchar(255),
        @attribute_value varchar(6000),
        @tmpAttrValue varchar(255),
        @first_section_id int;

    declare @primary_license varchar(255);
    select @primary_license = primaryLicense from pageData
    where url = @url;

    declare curSecAttr cursor for
    select attribute_cd,attribute_value from shs_page_section_attributes
    where url = @url and section_id = @section_id;  

    declare @show_only_new char(5);
    select @show_only_new = ISNULL(show_only_new, 'N')
    from shs_page_sections
    where url = @url 
    AND section_id = @section_id;

        insert into @tblCatAttrFilteredPhysicalItems
        select 
            @request_identifier,i.*
        from mf_item_detail i
        inner join mf_item_detail mi
        on i.master_guid = mi.item_guid
        inner join mf_item_categories ic
        on mi.item_guid = ic.item_guid
        where ic.category_guid in (select item from dbo.udfSplit(@category_guids,'#'))
        and mi.item_image_counter > 0
        and mi.item_discontinued = 0
        and mi.item_type_id = 3
        and i.item_quantity > 0

        UNION

        select
            @request_identifier,i.*
        from mf_item_detail i
        inner join mf_item_categories ic
        on i.item_guid = ic.item_guid
        where ic.category_guid in (select item from dbo.udfSplit(@category_guids,'#'))
        and i.item_image_counter > 0
        and i.item_discontinued = 0
        and i.item_type_id = 1
        and i.master_guid IS NULL
        and i.item_quantity > 0;

    select * into #tmpCatAttrFilteredItems from @tblCatAttrFilteredPhysicalItems;

    update #tmpCatAttrFilteredItems
    set item_color = mi.item_color,
    item_gender = mi.item_gender,
    item_holiday = mi.item_holiday,
    item_age_group = mi.item_age_group,
    item_licensor = mi.item_licensor,
    item_manufacturer = mi.item_manufacturer,
    item_material = mi.item_material,
    item_primary_license = mi.item_primary_license,
    item_series = mi.item_series,
    item_sleeve_type = mi.item_sleeve_type,
    item_softness = mi.item_softness,
    item_tshirt_attributes = mi.item_tshirt_attributes,
    item_promotion = mi.item_promotion 
    from mf_item_detail mi
    where mi.item_guid = #tmpCatAttrFilteredItems.master_guid
    and #tmpCatAttrFilteredItems.item_type_id = 1
    and #tmpCatAttrFilteredItems.master_guid is not null
    and request_identifier = @request_identifier;

    select * into #tblTmpResultSet from #tmpCatAttrFilteredItems where 1 = 2;

    open curSecAttr;
    fetch curSecAttr into @attribute_cd,@attribute_value;

    while(@@FETCH_STATUS = 0)
    begin
        if(@attribute_cd = 'AssociatedHero')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_associated_hero,','))
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_associated_hero,',')) 
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end

        if(@attribute_cd = 'Color')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_color,','))
                    and request_identifier = @request_identifier;                           

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_color,','))
                and request_identifier = @request_identifier;                           
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;                            ;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end         

        if(@attribute_cd = 'Gender')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_gender,','))
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_gender,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end                 

        if(@attribute_cd = 'Holiday')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_holiday,','))
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_holiday,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end 

        if(@attribute_cd = 'Promotion')
        begin
              if (charIndex(',',@attribute_value) > 0)
              begin
                    declare cur_AttrValue cursor for
                    select item from dbo.udfSplit(@attribute_value,',');

                    open cur_AttrValue;
                    fetch cur_AttrValue into @tmpAttrValue;
                    while (@@FETCH_STATUS = 0)
                    begin
                          insert into #tblTmpResultSet
                          select * from #tmpCatAttrFilteredItems
                          where @tmpAttrValue in (select item from dbo.udfSplit(item_promotion,','))
                          and request_identifier = @request_identifier;

                          fetch cur_AttrValue into @tmpAttrValue;
                    end
                    close cur_AttrValue;
                    deallocate cur_AttrValue;
              end
              else
              begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @attribute_value in (select item from dbo.udfSplit(item_promotion,','))
                    and request_identifier = @request_identifier;
              end

              delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

              insert into #tmpCatAttrFilteredItems
              select * from #tblTmpResultSet where request_identifier = @request_identifier;

              delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end

        if(@attribute_cd = 'IntendedAgeGroup')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_age_group,','))
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_age_group,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end                 

        if(@attribute_cd = 'Licensor')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_licensor,','))   
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_licensor,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end         

        if(@attribute_cd = 'Manufacturer')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_manufacturer,','))
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_manufacturer,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end                 

        if(@attribute_cd = 'Material')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_material,','))   
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_material,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end                         

        if(@attribute_cd = 'PrimaryLicense')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_primary_license,','))    
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_primary_license,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end 

        if(@attribute_cd = 'Series')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_series,',')) 
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_series,','))
                and request_identifier = @request_identifier;
            end         

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end     

        if(@attribute_cd = 'Size')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_size,','))   
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_size,','))
                and request_identifier = @request_identifier;
            end         

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end             

        if(@attribute_cd = 'SleeveType')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_sleeve_type,','))    
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_sleeve_type,','))
                and request_identifier = @request_identifier;
            end         

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;        
        end

        if(@attribute_cd = 'Softness')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_softness,','))   
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_softness,','))
                and request_identifier = @request_identifier;
            end         

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end     

        if(@attribute_cd = 'T-ShirtAttributes')
        begin
            if (charIndex(',',@attribute_value) > 0)
            begin
                declare cur_AttrValue cursor for
                select item from dbo.udfSplit(@attribute_value,',');

                open cur_AttrValue;
                fetch cur_AttrValue into @tmpAttrValue;
                while (@@FETCH_STATUS = 0)
                begin
                    insert into #tblTmpResultSet
                    select * from #tmpCatAttrFilteredItems
                    where @tmpAttrValue in (select item from dbo.udfSplit(item_tshirt_attributes,','))
                    and request_identifier = @request_identifier;

                    fetch cur_AttrValue into @tmpAttrValue;
                end
                close cur_AttrValue;
                deallocate cur_AttrValue;
            end
            else
            begin
                insert into #tblTmpResultSet
                select * from #tmpCatAttrFilteredItems
                where @attribute_value in (select item from dbo.udfSplit(item_tshirt_attributes,','))
                and request_identifier = @request_identifier;
            end

            delete from #tmpCatAttrFilteredItems where request_identifier = @request_identifier;

            insert into #tmpCatAttrFilteredItems
            select * from #tblTmpResultSet where request_identifier = @request_identifier;

            delete from #tblTmpResultSet where request_identifier = @request_identifier;
        end     

        fetch curSecAttr into @attribute_cd,@attribute_value;
    end
    close curSecAttr;
    deallocate curSecAttr;

    declare @tblFilteredItems table
    (
        request_identifier uniqueidentifier,
        item_guid varchar(255),
        item_cd varchar(25),
        master_guid varchar(255),
        item_quantity int,
        item_created datetime,
        item_approved int,
        sale_rank decimal(18,2) NULL,
        qoh_rank decimal(18,2) NULL,
        new_item_flag int NULL,
        item_rank decimal(18,2) NULL,
        is_item_new varchar(5)      
    );

    declare @tblFilteredOrderItems table
    (
        request_identifier uniqueidentifier,
        item_guid varchar(255),
        item_cd varchar(25),
        master_guid varchar(255),
        item_quantity int,
        order_item_quantity int,
        order_item_quantity_fulfilled int,
        item_created datetime,
        item_approved int,
        sale_rank decimal(18,2) NULL,
        qoh_rank decimal(18,2) NULL,
        new_item_flag int NULL,
        item_rank decimal(18,2) NULL,
        is_item_new varchar(5)      
    );

    declare @tblFinalResultItems table
    (
        request_identifier uniqueidentifier,
        item_guid varchar(255),
        item_cd varchar(25),
        master_guid varchar(255),
        item_quantity int,
        order_item_quantity int,
        order_item_quantity_fulfilled int,
        item_created datetime,
        item_approved int,
        sale_rank numeric(18,5) NULL,
        qoh_rank numeric(18,5) NULL,
        new_item_flag int NULL,
        item_rank numeric(18,5) NULL,
        is_item_new varchar(5)      
    );  

    declare 
        @TotNoOfItems int,
        @MaxOrderQuantity int,
        @MaxQtyOnHand int;

    insert into @tblFilteredOrderItems
    (request_identifier,item_guid,item_cd,master_guid,item_quantity,order_item_quantity,order_item_quantity_fulfilled,item_created,item_approved,is_item_new)
    select
        distinct @request_identifier,
        oi.item_guid,
        min(i.item_cd) as item_cd,
        i.master_guid as master_guid,
        sum(i.item_quantity) as item_quantity,
        sum(oi.order_item_quantity) as order_item_quantity,
        sum(oi.order_item_quantity_fulfilled) as order_item_quantity_fulfilled,
        MIN(i.item_created) as item_created,
        avg(i.item_approved) as item_approved,
        min(i.is_item_new) as is_item_new
    from mf_order_items oi
    inner join #tmpCatAttrFilteredItems i
    on i.item_guid = oi.item_guid
    inner join mf_orders o
    on o.order_guid = oi.order_guid
    where o.order_date between getdate()-@NoOfDaysToRank and getdate()
    and i.master_guid is NULL
    group by oi.item_guid,i.master_guid

    UNION

    select
        distinct @request_identifier,
        mi.item_guid,
        min(mi.item_cd) as item_cd,
        mi.master_guid as master_guid,
        sum(i.item_quantity) as item_quantity,
        sum(oi.order_item_quantity) as order_item_quantity,
        sum(oi.order_item_quantity_fulfilled) as order_item_quantity_fulfilled,
        MIN(mi.item_created) as item_created,
        min(mi.item_approved) as item_approved,
        min(mi.is_item_new) as is_item_new
    from mf_order_items oi
    inner join #tmpCatAttrFilteredItems i
    on i.item_guid = oi.item_guid 
    inner join mf_item_detail mi
    on mi.item_guid = i.master_guid 
    inner join mf_orders o
    on o.order_guid = oi.order_guid 
    where o.order_date between getdate()-@NoOfDaysToRank and getdate()
    and i.master_guid is NOT NULL   
    group by oi.item_guid,mi.item_guid,mi.master_guid;

    insert into @tblFilteredItems   
    (request_identifier,item_guid,item_cd,master_guid,item_quantity,item_created,item_approved,is_item_new)
    select
        distinct @request_identifier,
        item_guid,
        item_cd as 

14 个答案:

答案 0 :(得分:16)

嗯,请快速阅读,但有两件事情会立即浮现在脑海中:

  • 将其分解为更小的程序!这将使维护工作变得更加容易。
  • 它使用游标。也许有一种方法可以在没有游标的情况下解决相同的问题。恕我直言,如果可能的话,应该避免使用游标(特别是因为cursos违反了SQL Server的基于集合的方法,因为它们很慢)。

答案 1 :(得分:10)

除了现有的答案之外,我还想补充一些让我感到惊讶的东西,但是还没有人指出:

  • 没有评论。

答案 2 :(得分:6)

太长了。

答案 3 :(得分:5)

作为一般原则,T-SQL是一种声明性语言,即你告诉它你想要什么,而SQL Server(我假设)会弄清楚如何得到结果。

我的主要批评不是“长度”,“游标”(本身,虽然它们是问题的第一个症状)或“慢”(没有数据集,所以不能说)。

程序写得强制性 - 它正在告诉SQL究竟要做什么,以什么顺序,以获得所需的结果,就像在C#中编写程序一样。我看到了游标,我想:这个程序员还没有完全掌握SQL的基于集合的特性。这至少会导致两个问题:

  1. 可读性 - 这不仅仅是长度的函数,也是风格的函数。如果它在功能上被分解(视图,CTE),那么数据集如何构建并组合到最终结果将更清楚。这无法避免不可原谅的缺乏评论。

  2. 性能将更难调整,因为您不允许SQL优化器发挥其魔力 ​​- 查看行计数,连接顺序等,并重新排列方法以最有效的方式获得结果。你让发动机无法完成它的工作。这并不是说它在100%的时间内都是正确的,并且不需要特定的调整,缓存,暗示等等,但是从功能性的声明性版本开始并从那里进行调整更容易。

    < / LI>

答案 4 :(得分:4)

  1. 太长了 - 我在阅读时感到无聊:(。

  2. 格式化使难以阅读(但是 这取决于我个人的观点和 体验)。

  3. 没有评论,这使得难以阅读并理解。

  4. 我找不到源代码,但是大的执行计划(当然这个庞大的存储过程可能有一个很大的执行计划)没有缓存,这意味着每次执行导致新的重新编译,并可能导致性能问题

  5. 下面:

    declare curSecAttr cursor for
    select attribute_cd,attribute_value from
    shs_page_section_attributes
    where url = @url and section_id = @section_id;
    

    光标不是LOCA L - 这可以 如果两个SP 同时运行会导致错误 - 详细信息请参见[MSDN:Transact-SQL游标名称的范围](http://msdn.microsoft.com/en-us/library/ms189238(lightweight).aspx)

  6. 这里:

    declare @show_only_new char(5);
    select @show_only_new = ISNULL(show_only_new, 'N')
    

    @show_only_new的值将为“N____”,其中_为空格,如果列show_only_new的值为空。在过去,我看到了几个与VARCHAR变量的比较,在代码中引入了细微的错误

  7. SP创建了许多没有索引的本地和临时表(临时表可以使用)。这可能影响效果

  8. 似乎属性@attribute_cd的值硬编码。这是不好的做法 ......

  9. 似乎有很多重复代码应该重构(即分成更小的存储过程)。

  10. 在这长段代码中可能隐藏着很多其他问题和流程,这是它的下一个坏处......

答案 5 :(得分:2)

你想让我们同意你这很可怕吗?

如果它符合您的要求,那么我看不出问题所在。

答案 6 :(得分:1)

我担心你的承包商的客户不知道有足够的SQL来自己生成存储过程,或者购买他能找到的最便宜的承包商,然后当他得到他所支付的费用时抱怨。

答案 7 :(得分:1)

<强> TL;博士

认真 - 你的主要抱怨是什么?它不符合要求吗?是否需要更好地记录?

答案 8 :(得分:1)

所有这些IF块看起来都很重复。我怀疑可以将所有内容重写为单个语句,也许没有游标。但如果性能不是问题,并且它有效,我看不出任何明显的错误。如上所述,它很长,但使用表变量,因此将它分解为更小的程序会很尴尬

答案 9 :(得分:0)

程序名称令人震惊,它做了什么?

参数太多,因为指南7的参数是很好的限制。

没有评论。

使用游标可能表明作者确实理解数据库编程。

没有错误处理。

将它拆分成几个较小的存储过程可能是有意义的,虽然我怀疑摆脱游标并且一些注释会使它更容易理解。

答案 10 :(得分:0)

基于快速回顾,我首先要问的是大量的IF块。它们看起来都很相似,应该合并。而且,一般来说,它是ssssssssoooooooooooo很长。

我也不喜欢使用区分大小写的If语句。因为你忘了在某些环境中把第一个字母大写,所以太容易搞砸了。

答案 11 :(得分:0)

批评?

它丑陋而且难以维护。太长了。它的结构很差。看起来它在一次会议中很快就被淘汰了。它看起来会很慢。它看起来很容易打破。

答案 12 :(得分:0)

检查数据库设计,似乎它不在第3范式中。有些属性是多值的,最重要的是它们以逗号分隔。不好!这使程序更糟糕。

此外,除了udfSplit的第一个参数外,大约有13个段落(每个属性一个)90%结构相同。重构它们,程序大小(和复杂性)将急剧下降。

答案 13 :(得分:0)

贵公司似乎正在招聘错误的人。编写如此漫长,未注释,粗略存储过程的人。反过来又由在互联网上发布过程的人管理,以找出问题所在。严重。