Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


please help me tunnig part of below query?


please help me tunnig part of below query?

Author
Message
santoshpatil.kumar
santoshpatil.kumar
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 3
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
rhythm.varshney
rhythm.varshney
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 195
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.
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6108 Visits: 6080
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.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16674 Visits: 17033
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36357 Visits: 18757
I would tend to agree with Sean. Likely this could be rewritten to function much better and quicker.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search