Urgent Help!!! on Function

  • Urgent help please

    Ways to Improve this function:

    CREATE FUNCTION [dbo].[LBS_Quotes_GetEarliestCurrentVariantCode]

    (

    @p_quote_idINT

    )

    RETURNS VARCHAR(1)

    AS

    BEGIN

    -- declare temporary variables

    DECLARE@ResultVARCHAR(1),

    @count_oldINT,

    @count_preINT

    -- count number of old and pre-current vehicles in the quote

    SELECT@count_old= SUM(case when v.current_variant = 'O' then 1 else 0 end),

    @count_pre= SUM(case when v.current_variant = 'P' then 1 else 0 end)

    FROM [quote_vehicles] qv

    INNER JOIN [prdvref]..[variants] vON v.manufacturer_id = qv.manufacturer_id and v.model_id = qv.model_id and v.variant_id = qv.variant_id

    WHERE qv.quote_id = @p_quote_id

    -- determine return value

    IF (@@ERROR = 0)

    BEGIN

    IF (@count_old > 0) SET @Result = 'O'

    ELSE IF (@count_pre > 0) SET @Result = 'P'

    ELSE SET @Result = ''

    END

    RETURN @Result

    END

    GO

  • What's wrong with it (other than slow, which it is because it's a function)?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • pespes009 (9/29/2011)


    Urgent help please

    Ways to Improve this function:

    CREATE FUNCTION [dbo].[LBS_Quotes_GetEarliestCurrentVariantCode]

    (

    @p_quote_idINT

    )

    RETURNS VARCHAR(1)

    AS

    BEGIN

    -- declare temporary variables

    DECLARE@ResultVARCHAR(1),

    @count_oldINT,

    @count_preINT

    -- count number of old and pre-current vehicles in the quote

    SELECT@count_old= SUM(case when v.current_variant = 'O' then 1 else 0 end),

    @count_pre= SUM(case when v.current_variant = 'P' then 1 else 0 end)

    FROM [quote-0_vehicles] qv

    INNER JOIN [prdvref]..[variants] vON v.manufacturer_id = qv.manufacturer_id and v.model_id = qv.model_id and v.variant_id = qv.variant_id

    WHERE qv.quote_id = @p_quote_id

    -- determine return value

    IF (@@ERROR = 0)

    BEGIN

    IF (@count_old > 0) SET @Result = 'O'

    ELSE IF (@count_pre > 0) SET @Result = 'P'

    ELSE SET @Result = ''

    END

    RETURN @Result

    END

    GO

    [/quote-0]

    It seems based off your last statement in the code that you could do something more like this:

    if exists(SELECTv.current_variant = 'O'

    FROM [quote_vehicles] qv

    INNER JOIN [prdvref]..[variants] vON v.manufacturer_id = qv.manufacturer_id and v.model_id = qv.model_id and v.variant_id = qv.variant_id

    WHERE qv.quote_id = @p_quote_id and v.current_variant = 'O' )

    begin

    set @result = 'O'

    end

    else if exists(SELECTv.current_variant = 'P'

    FROM [quote_vehicles] qv

    INNER JOIN [prdvref]..[variants] vON v.manufacturer_id = qv.manufacturer_id and v.model_id = qv.model_id and v.variant_id = qv.variant_id

    WHERE qv.quote_id = @p_quote_id and v.current_variant = 'P')

    begin

    set @result = 'P'

    end

    else begin

    set @result = ''

    end

    It appears based off your existing code that you don't actually care about the count, if O exists then it is an O if P exists then P if neither exist then blank.

    Ben

  • It looks to me like you could use an existence query instead of count() since you aren't really interested in the actual count, just that it is > 0.

    "if exists() " can be a lot faster than "select count()..." in actual practice for obvious reasons.

    The probability of survival is inversely proportional to the angle of arrival.

  • Thanks a lot guys !!!

  • pespes009 (9/29/2011)


    Thanks a lot guys !!!

    So, what did you end up with? Two way street here... Please post the code that you ended up using so that others may learn. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • How is this function being used; as a column in a SELECT statement, a WHERE clause of a SELECT statement?

    There is probably another way to write the function as well.

    What would help is the DDL for the tables, some sample data for the tables, and the expected results based on the sample data.

    Read the first article I reference below in my signature block to see how you should post this information if you are interested in any other alternatives.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply