
Esta sp é para usar para actualizar consumo de horas numa aplicação
--
-- Cria a SP com o nome sp_actualiza_onsite
--
create procedure [dbo].[sp_actualiza_onsite]
as Begin
--
-- Insere Ticket depois de Fechado (Estado 'Closed')
-- Uso um Insert a partir de um Select
--
INSERT INTO [HEAT].[dbo].[Tickets] ([CallId] ,[CallType] ,[CriData] ,[Valor] ,[Saldo] ,[UIDMANUT])
select d.callid,c.calltype,c.closeddate, d.horas_gastas, d.horas_saldo
,d.uidmanut,c.custid
from detail d
inner join calllog c on c.callid=d.callid
inner join subset s on s.callid=c.callid
inner join config cfg on cfg.u_idreg=d.uidmanut
where (c.calltype='Onsite' or c.calltype='Packs') and c.callstatus='Closed' and c.actualiza<>'SIM'
and c.callid not in (select callid from tickets )
--
-- Actualiza contratos com as Horas Gastas
-- Cursor usado para update
--
-- Declaração do Cursor com o nome de cursor_Tickets
-- Select aonde vai correr o cursor
DECLARE Cursor_tickets CURSOR for
SELECT t.CallId,c.custid,t.Valor,t.UIDMANUT
from tickets t
inner join calllog c on c.callid=t.callid
where t.[CallType]='Onsite' and c.actualiza <>'SIM'
--
-- Declaração de Variaveis para passar do Cursor
--
Declare @Callid varchar(8)
declare @custid varchar(50)
declare @valor decimal(17,2)
declare @uidmanut varchar(25)
set @callid=''
set @custid=''
set @valor=0
set @uidmanut=''
Open Cursor_tickets /* abrir o cursor */
fetch next from Cursor_tickets
into @callid,@custid,@valor,@uidmanut
while @@fetch_Status=0
begin
--- Actualização das Horas Gastas
--- select @valor,@custid,@uidmanut (usado para verificação e teste)
update config set horas_gastas=isnull(@valor,0)+(isnull(horas_gastas,0)) ,horas_saldo=isnull(horas_saldo,0)-isnull(@valor,0)
where u_idreg=@uidmanut and custid=@custid
set @callid=''
set @custid=''
set @valor=0
set @uidmanut=''
fetch next from Cursor_Tickets
into @callid,@custid,@valor,@uidmanut
end
close Cursor_tickets
Deallocate Cursor_tickets
--
-- No Final actualiza a tabela de forma a não repetir os registos já processados
--
update calllog set actualiza='SIM' where callid in (select callid from tickets) and actualiza<>'SIM'
end
Sem comentários:
Enviar um comentário