Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 dynamic vs compiled

Author  Topic 

fisherman_jake
Slave to the Almighty Yak

159 Posts

Posted - 2002-03-07 : 17:54:05
Gurus, hi, I just want to find out your opinion on this pseudocode:

1. Get Start and End dates
2. While Start <= End Do
2a. Delete from target table where date = Start
2b. Insert into target table source values where date = Start
2c. Increment Start by 1 month
2d. Remember start date for progressive import

Ok fairly easy enough, now what are the implications if:

a. I encapsulate 2a to 2c within a transaction (ie begin tran, commit tran, isolation level serializable)
b. I encapsulate 2a to 2c within a transaction and have 2b as dynamic SQL
c. have 2b as dynamic SQL
d. leave as is

My own findings:
a. slower than c or d, don't get data in when stopped
b. slower than c or d, don't get data in when stopped
c. leaves data in target when stopped
d. data is rolled back out

Thanks in advance!!


==================================================
Do not argue with IDIOTS. They will take you down to their level and BEAT you with experience.
Master Fisherman

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-03-07 : 17:58:08
It sounds like you are importing monthly data files, but need to clean out the data for each month before (re)importing it. Yes? No?

Since it sounds like you've got some code for this, can you post it?

Go to Top of Page

fisherman_jake
Slave to the Almighty Yak

159 Posts

Posted - 2002-03-07 : 18:30:29
Ok, you asked for it..


--<TSQL>--
...
If Not Exists(Select * From [Central Staging]..ProcessStepData where SpID = @@ProcID)
Begin
Insert [Central Staging]..ProcessStepData Values (@@ProcID, Null, Null, @StartDate)
End
Else
Begin
-- Only delete last step from the table
Select @StartDate = DateValue From [Central Staging]..ProcessStepData where SpID = @@ProcID
End

Set Transaction Isolation Level Serializable

While (@StartDate <= @EndDate) And @tiDudCounter < 5
Begin

Begin Transaction

Delete Central.Dbo.IbesEstMonthEndData
Where DateMonthEnd = @StartDate
And (((MeasureID = @iMeasure) And (@iMeasure Is Not Null)) Or (@iMeasure Is Null))

INSERT INTO Central.Dbo.IbesEstMonthEndData
Select M.EntityID, M.BrokerID, M.AnalystID, M.CurrID, M.EpiID, M.MeasureID,
M.Value, M.AdjFactor, M.DatePeriodEnd, D.DateMonthEnd, M.DateEstimate
FROM
Central.dbo.IbesEstData M
Inner Join
(Select E.EntityID, E.BrokerID, E.AnalystID, E.CurrID, E.EpiID,
E.MeasureID, E.DatePeriodEnd, max(E.DateEstimate) as DateEstimate,
@StartDate as DateMonthEnd
From
Central.dbo.IbesEstData E
Left Join ( Select EntityID, DatePeriodEnd, DateReported
From
Central.dbo.IbesActualData
Where
FreqID = 1 and DateReported is not null
And (((MeasureID = @iMeasure) And ( @iMeasure is not null)) Or (@iMeasure is null))) A
On E.EntityID = A.EntityID and E.DatePeriodEnd = A.DatePeriodEnd
Where
(((MeasureID = @iMeasure) And ( @iMeasure is not Null )) Or (@iMeasure is null))
-- The most important condition here
-- Period End Date + 2 months MUST be >= the Month End(StartDate) AND
-- Estimate Date is within 2 months of the Month End(StartDate) and the Report Date is >= the Month End
-- OR If the Month End is NULL then Estimate Date is within 2 months of the Month End
AND (((datediff(m, E.DateEstimate, @StartDate) <= 2) and A.DateReported >= @StartDate and (A.DateReported is not null))
OR
((datediff(m, E.DateEstimate, @StartDate) <= 2) and (A.DateReported is null))
And (Dateadd(m, 2, E.DatePeriodEnd) >= @StartDate ))
Group BY E.EntityID, E.BrokerID, E.AnalystID, E.CurrID, E.EpiID, E.MeasureID, E.DatePeriodEnd ) D
ON
M.EntityID = D.EntityID
AND M.BrokerID = D.BrokerID
AND M.AnalystID = D.AnalystID
AND M.CurrID = D.CurrID
AND M.EpiID = D.EpiID
AND M.MeasureID = D.MeasureID
AND M.DatePeriodEnd = D.DatePeriodEnd
And M.DateEstimate = D.DateEstimate

If @@RowCount = 0
Set @tiDudCounter = @tiDudCounter + 1
Else
Set @tiDudCounter = 0

Commit Transaction


select @StartDate = Dateadd(dd,-1, dateadd(mm, 2, cast(year(@StartDate) as varchar(4)) + '/' + cast(month(@StartDate) as varchar (2)) + '/01'))

Update [Central Staging]..ProcessStepData
Set IntValue = Null,
StrValue = Null,
DateValue = @StartDate
where SpID = @@ProcID

End

...
--<TSQL>--


Basically the I tried the dynamic SQL on the INSERT statement without the begin/commit tran. What do you think?



==================================================
Do not argue with IDIOTS. They will take you down to their level and BEAT you with experience.
Master Fisherman
Go to Top of Page

fisherman_jake
Slave to the Almighty Yak

159 Posts

Posted - 2002-03-07 : 18:35:08
Rob,
Sorry, mate, the answer to Do I have to delete it per the month is yes, because if the process have to be stopped I want to have data in the table. I'm not importing monthly files, the data is coming from another table, but it's like making a monthly summary from the source table. Hence the process needs to be done monthly..


==================================================
Do not argue with IDIOTS. They will take you down to their level and BEAT you with experience.
Master Fisherman
Go to Top of Page
   

- Advertisement -