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 2008 Forums
 Transact-SQL (2008)
 Better Faster solution update

Author  Topic 

fan2005
Yak Posting Veteran

85 Posts

Posted - 2011-05-17 : 09:37:29
Hi ,
I'm looking for an alternative query for this one.
It's runing on 1000000 rows and is realy slow
I coundnt' make cursor to work with dynamic sql although I guess cursor is alsow slow.
I didn't create index on ID column .If it'll help what kind should it be?
(CLUSTERED /NON CLUSTERED )
Thanks




declare @total int
set @total=(select max(id) from cashflow_1month_interests)

declare @row int
set @row=1

DECLARE @pTDOPNDAT varchar(10)
declare @TBTEMPRATE float
DECLARE @TDOPNAMT decimal(20,0)
DECLARE @pdate nvarchar(10)
DECLARE @interest decimal(20,0)
DECLARE @sql varchar(4000)
DECLARE @TFDPDURATN int
DECLARE @i int


while @row<=@total
begin

set @TDOPNAMT =(select cast (TDOPNAMT as decimal(20,0) ) from cashflow_1month_interests where id=@row)
set @TBTEMPRATE =(select cast (TBTEMPRATE as float )from cashflow_1month_interests where id=@row)
set @TFDPDURATN=(select cast(TFDPDURATN as int) from cashflow_1month_interests where id=@row)
set @pdate=(select PTDOPNDAT from cashflow_1month_interests where id=@row)

set @i =1
while @i<= @TFDPDURATN
begin

set @interest = @TDOPNAMT*(@TBTEMPRATE)*
dbo.ShamsiDateDiff (@pdate,dbo.nextMonth(@pdate))/36500

if @i< cast(@TFDPDURATN as int)
begin
set @sql='UPDATE cashflow_1month_interests SET intersts_of_month'+cast(@i as varchar(2))+' ='+cast( @interest as nvarchar(255))+' where id='+cast (@row as nvarchar(255))
print( @sql )
exec (@sql)
end
if @i= cast(@TFDPDURATN as int)
begin
set @sql='UPDATE cashflow_1month_interests SET intersts_of_month'+cast(@i as varchar(2))+' = '+cast( @interest+@TDOPNAMT as nvarchar(255))+' where id='+cast (@row as nvarchar(255))
print( @sql )
exec (@sql)
end

set @pdate=dbo.nextMonth(@pdate)
set @i=@i+1
end
set @row=@row+1
end

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-05-17 : 09:41:16
Dupe: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=160683
Go to Top of Page
   

- Advertisement -