please help me tunnig part of below query?

  • USE [PD_ODS]

    GO

    /****** Object: StoredProcedure [dbo].[sp_replicate_nxtdate] Script Date: 12/13/2012 13:44:18 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE procedure [dbo].[sp_replicate_nxtdate]

    @p_siteid varchar(100)

    as

    begin

    declare @pmnum varchar(100)

    declare @description varchar(400)

    declare @assetnum varchar(100)

    declare @assetname varchar(100)

    declare @failurecode varchar(100)

    declare @jpnum varchar(100)

    declare @craft varchar(100)

    declare @frequnit varchar(100)

    declare @frequency int

    declare @jpduration decimal(10,2)

    declare @actual_hours decimal(10,2)

    declare @key varchar(100)

    declare @new_key varchar(100)

    declare @siteid varchar(100)

    declare @orgid varchar(100)

    declare @version int

    declare @changedate datetime

    declare @change_user varchar(30)

    declare @nextdate datetime

    declare @extdate varchar(20)

    declare @rpct_f char(1)

    declare @currdate datetime

    declare @v1_month int

    declare @v2_week int

    declare @v3_year int

    declare @v4_day int

    declare @vw int

    declare @VM int

    declare @vy int

    declare @VD int

    --cursor declaration--

    declare cursor_mstr_extr CURSOR for

    select * from dbo.PRVN_MNTE_MSTR_EXTR where version=1 and siteid=@p_siteid order by pmnum --and pmnum=33473

    --open cursor--

    --delete from replica where next_date is not null

    set @v1_month=24

    set @v2_week=104

    set @v3_year=2

    set @v4_day=730

    open cursor_mstr_extr

    fetch next from cursor_mstr_extr into @pmnum,@description,@assetnum,@assetname,@failurecode,@jpnum,

    @craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,

    @orgid,@version,@changedate,@change_user,@nextdate,@extdate,@rpct_f

    if @pmnum is not null and @frequnit is not null

    and @frequency is not null and @nextdate is not null

    begin

    ---declare @currdate datetime---

    WHILE @@FETCH_STATUS = 0

    begin

    set @VM=1

    set @vw=1

    set @vy=1

    set @VD=1

    select @currdate=min(nextdate) from PRVN_MNTE_MSTR_EXTR where pmnum=@pmnum

    if @frequnit like 'MONTH%'

    begin

    while @VM<=@v1_month/@frequency and @nextdate<dateadd(yyyy,2,@currdate)

    begin

    set @nextdate=(dateadd(mm,@frequency,@nextdate))

    insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,

    @jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,

    @orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )

    --set @cdate=@next_date

    set @VM=@vm+1

    end

    end

    else

    if @frequnit like 'WEEK%'

    begin

    while @vw<=@v2_week/@frequency and @nextdate<dateadd(yyyy,2,@currdate)

    begin

    set @nextdate=(dateadd(wk,@frequency,@nextdate))

    insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,

    @jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,

    @orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )

    --set @cdate=@next_date

    set @vw=@vw+1

    end

    end

    else

    if @frequnit like 'YEAR%'

    begin

    while @vy<=@v3_year/@frequency and @nextdate<dateadd(yyyy,2,@currdate)

    begin

    set @nextdate=(dateadd(yyyy,@frequency,@nextdate))

    insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,

    @jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,

    @orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )

    --set @cdate=@next_date

    set @vy=@vy+1

    end

    end

    else

    if @frequnit like 'DAY%'

    begin

    while @VD<=@v4_day/@frequency and @nextdate<dateadd(yyyy,2,@currdate)

    begin

    set @nextdate=(dateadd(dd,@frequency,@nextdate))

    insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,

    @jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,

    @orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )

    --set @cdate=@next_date

    set @VD=@vd+1

    end

    end

    else break

    ---fetch next row---

    fetch next from cursor_mstr_extr into @pmnum,@description,@assetnum,@assetname,@failurecode,@jpnum,

    @craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,

    @orgid,@version,@changedate,@change_user,@nextdate,@extdate,@rpct_f

    end --end of WHILE

    end --end of if

    close cursor_mstr_extr

    DEALLOCATE cursor_mstr_extr

    end

    GO

  • Please refer below URL an article from Jeff Moden where he mentioned how to use Tally Table.

    http://www.sqlservercentral.com/articles/T-SQL/62867/

    Also just a suggestion don't use 'sp' as prefix for your user defined stored procs.

  • Welcome to SCC

    Can you please follow the links in my signature to posting, code and data for the best help and also the how to post performance problems so that we can help you.

    But at a quick qlance, get rid of the cursor if you can.

  • anthony.green (12/19/2012)


    Welcome to SCC

    Can you please follow the links in my signature to posting, code and data for the best help and also the how to post performance problems so that we can help you.

    But at a quick qlance, get rid of the cursor if you can.

    I agree with reading that article but I think the change needs to a bit more radical. You need a complete rewrite of this process. You have a cursor with at least four while loops for each iteration through the cursor. I would bet that once you post some ddl, sample data and desired output we can help you turn this monstrosity into a single insert statement.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I would tend to agree with Sean. Likely this could be rewritten to function much better and quicker.

Viewing 5 posts - 1 through 4 (of 4 total)

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