Home Forums SQL Server 2008 T-SQL (SS2K8) Convert the stored procedure to "Standard SQL Select" RE: Convert the stored procedure to "Standard SQL Select"

  • halifaxdal (4/11/2014)


    This is driving people crazy!

    There is another sp to be converted and this sp contains loop and lots of calculation. The original sp is working fine and logic is very simple, can I have advice from anyone?

    Create proc [dbo].[ddGetDevDRROnly]

    AS

    Declare @q int --variable for quarter

    Declare @Balance int

    Declare @SVP varchar(20)

    Declare @b-2 int --Open Deviations as of Oct 31

    Declare @C int --New dev with original TargetClosureDate within current fiscal year

    Declare @D int --New dev with original TargetClosureDate beyong current fiscal year

    Declare @a int --YTD number of retired dev

    Declare @DRR float --A/(B+C+D)

    Create table #temp

    (

    Division varchar(20),

    B int,

    C int,

    D int,

    A int,

    DRR float

    )

    --Loop each Division

    declare cDivision cursor for

    Select Division From ITSDivision

    OPEN cDivision

    FETCH NEXT FROM cDivision

    INTO @SVP

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SELECT @b-2 = count(1) FROM Document d

    left outer join ITSDivision i on d.ITSDivisionID = i.ID

    Where

    i.Division = @SVP and

    DocStatus = 'Active' and

    DocType = 1

    SELECT @C = count(1) FROM Document d

    left outer join ITSDivision i on d.ITSDivisionID = i.ID

    Where

    i.Division = @SVP and

    DocStatus = 'Active' and

    DocType = 1 and

    CAST(YEAR(TargetClosureDate) AS int) = dbo.fnFiscalYear(getdate())

    SELECT @D = count(1) FROM Document d

    left outer join ITSDivision i on d.ITSDivisionID = i.ID

    Where

    i.Division = @SVP and

    DocStatus = 'Active' and

    DocType = 1 and

    CAST(YEAR(TargetClosureDate) AS int) > dbo.fnFiscalYear(getdate())

    SELECT @a = count(1) FROM Document d

    left outer join ITSDivision i on d.ITSDivisionID = i.ID

    Where

    i.Division = @SVP and

    DocStatus = 'Retired' and

    DocType = 1 and

    CAST(YEAR(RetiredDate) AS int) = (dbo.fnFiscalYear(getdate())-1)

    IF ((@B + @C + @D)> 0)

    BEGIN

    Set @DRR = CONVERT(DECIMAL(8,1), 100.0) * @a /(@B + @C + @D)

    END

    ELSE

    BEGIN

    Set @DRR = 0

    END

    Insert into #temp Values(@SVP, @b-2, @C, @D, @a, @DRR)

    FETCH NEXT FROM cDivision

    INTO @SVP

    END

    CLOSE cDivision

    DEALLOCATE cDivision

    select * from #temp order by DRR desc

    Yes... since the logic is "simple", I strongly recommend that you give it a shot on your own because you're likely the one that's going to have to support it. At least try. 😉 The best advice I can give to anyone trying to make such changes is in my signature line below about what to think about.

    --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)