Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

please help me tunnig part of below query? Expand / Collapse
Author
Message
Posted Tuesday, December 18, 2012 11:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 19, 2012 11:28 PM
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



Post #1398200
Posted Wednesday, December 19, 2012 1:59 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, September 19, 2013 4:30 AM
Points: 78, 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.
Post #1398237
Posted Wednesday, December 19, 2012 2:02 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
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
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

Post #1398239
Posted Wednesday, December 19, 2012 8:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1398456
Posted Wednesday, December 19, 2012 9:04 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 5:38 PM
Points: 31,368, Visits: 15,834
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
Post #1398481
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse