I have a procedure it takes long time to run this procedure
alter procedure test as
declare @Id int,@Type char(10),@total_created int,@total_orders_created int,@total_completed int,@log_total int,@log_aging int
declare c1 cursor for
select Id,Type
from temp1
open c1
fetch next from c1 into @id,@type
WHILE (@@FETCH_STATUS <> -1)
begin
IF (@@FETCH_STATUS <> -2)
select @total_created = count(tempfinal.id)
from tempfinal,temp1
where tempfinal.id = @id
and tempfinal.type = @type
and tempfinal.id = temp1.id
and tempfinal.type = temp1.type
select @total_orders_created = count(tempfinal.id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.type = @type
and tempfinal.id = temp1.id
and tempfinal.type = temp1.type
and tempfinal.status ='TD'
select @total_completed = count(tempfinal.id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.Type = @Type
and tempfinal.Id = temp1.Id
and tempfinal.Type = temp1.Type
and tempfinal.status ='TO'
select @log_total = count(tempfinal.Id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.Type = @Type
and tempfinal.Id = temp1.Id
and tempfinal.Type = temp1.Type
and tempfinal.status NOT IN('TO')
select @log_aging = count(tempfinal.Id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.Type = @Type
and tempfinal.Id = temp1.Id
and tempfinal.Type = temp1.Type
and tempfinal.wo_status NOT IN('TO')
and Datediff(day,tempfinal.PDate,getdate()) <=45
update temp1
set total_records = @total_created,
created =@total_orders_created,completed =@total_completed,
log_Total = @log_total, log_aging = @log_aging
where Id = @Id
and Type = @Type
fetch next from c1 into @Id,@Type
end
close c1
deallocate c1
Each variable in the cursor taking time, is there any way I can write this query bettter for performance point of view
alter procedure test as
declare @Id int,@Type char(10),@total_created int,@total_orders_created int,@total_completed int,@log_total int,@log_aging int
declare c1 cursor for
select Id,Type
from temp1
open c1
fetch next from c1 into @id,@type
WHILE (@@FETCH_STATUS <> -1)
begin
IF (@@FETCH_STATUS <> -2)
select @total_created = count(tempfinal.id)
from tempfinal,temp1
where tempfinal.id = @id
and tempfinal.type = @type
and tempfinal.id = temp1.id
and tempfinal.type = temp1.type
select @total_orders_created = count(tempfinal.id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.type = @type
and tempfinal.id = temp1.id
and tempfinal.type = temp1.type
and tempfinal.status ='TD'
select @total_completed = count(tempfinal.id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.Type = @Type
and tempfinal.Id = temp1.Id
and tempfinal.Type = temp1.Type
and tempfinal.status ='TO'
select @log_total = count(tempfinal.Id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.Type = @Type
and tempfinal.Id = temp1.Id
and tempfinal.Type = temp1.Type
and tempfinal.status NOT IN('TO')
select @log_aging = count(tempfinal.Id)
from tempfinal,temp1
where tempfinal.Id = @Id
and tempfinal.Type = @Type
and tempfinal.Id = temp1.Id
and tempfinal.Type = temp1.Type
and tempfinal.wo_status NOT IN('TO')
and Datediff(day,tempfinal.PDate,getdate()) <=45
update temp1
set total_records = @total_created,
created =@total_orders_created,completed =@total_completed,
log_Total = @log_total, log_aging = @log_aging
where Id = @Id
and Type = @Type
fetch next from c1 into @Id,@Type
end
close c1
deallocate c1
Each variable in the cursor taking time, is there any way I can write this query bettter for performance point of view