Good day everyone, I hope and someone has worked with my requirement. I have a query which is executed inside an SP, I would like it to be inside a condition (IF) and that if there is an error in the execution, it will send me an adventencia message by console. Thank you very much.
alter procedure sp_reporte_instalacion
(
---variables para crear calendario
@fecha_inicio datetime,
@fecha_fin datetime,
--variables para generar el reporte
@fecha_ini_repor datetime,
@fecha_fin_repor datetime
)
as
declare @fecha_inicial datetime,
@fecha_final datetime,
@fecha_actual datetime
declare @total int
set @fecha_inicial = @fecha_inicio
set @fecha_final = @fecha_fin
set @fecha_actual = @fecha_inicial
create table #temp_calendar
(weekday varchar(10),
date int,
month varchar(10),
year int)
while @fecha_actual <= @fecha_final
begin
insert into #temp_calendar
select
datename(dw, @fecha_actual),
datepart(day, @fecha_actual),
datename(month, @fecha_actual),
datepart(year, @fecha_actual)
set @fecha_actual = dateadd(dd, 1, @fecha_actual)
end
;
----REPORTE
Select datepart(day,sh.[Order Date]) as Fecha,
sh.[Responsibility Center],
(select COUNT ([Unit of Measure Code])
from [Pointer Mexico SA$Sales Invoice Line]
where [Document No_]=sih.[No_]
and [Unit of Measure Code]='DIA'
and (charindex('S-HANDS FREE',upper([No_]))<=0)
and(charindex('GARAN',upper([No_]))<=0)
and(charindex('DEMO',upper([No_]))<=0))Vendidas,
(select COUNT ([Unit of Measure Code])
from [Pointer Mexico SA$Sales Line]
where [Document No_]=sh.No_ and [Pre Service Start Date]<>'1753-01-01 00:00:00.000' and [Unit of Measure Code]='DIA')Instaladas
into #temp_Temporal
from [Pointer Mexico SA$Sales Header] sh
inner join [Pointer Mexico SA$Customer] c on sh.[Sell-to Customer No_] =c.[No_]
inner join [Pointer Mexico SA$Sales Invoice Header] sih on sh.[No_]=[Order No_]
where sih.[Order Date] between @fecha_ini_repor and @fecha_fin_repor and sh.[Shortcut Dimension 1 Code] in ('DV10','DV30')
--where sih.[Order Date] between @fecha_inicial and @fecha_final and sh.[Shortcut Dimension 1 Code] in ('DV10','DV30')
and ((charindex('DEMO',upper(sh.[External Document No_])) <= 0) AND (charindex('RENO',upper(sh.[External Document No_])) <= 0)
and (charindex('CESI',upper(sh.[External Document No_])) <= 0)and (charindex('GARAN',upper(sh.[External Document No_])) <= 0) )
and sih.[Reason Code]<>'GARANTIAS' and sh.[Tipo de Venta]<>'ACCESORIOS'
order by Fecha asc
;with Transp as (
select Fecha
,sum(Vendidas) as Total_Vendidas
,sum(Instaladas) as Total_Instaladas
,case when [Responsibility Center] = 'CMEX' then sum(Vendidas) end as Vendidas_CMEX
,case when [Responsibility Center] = 'CMTY' then sum(Vendidas) end as Vendidas_CMTY
,case when [Responsibility Center] = 'CMEX' then sum(Instaladas) end as Instaladas_CMEX
,case when [Responsibility Center] = 'CMTY' then sum(Instaladas) end as Instaladas_CMTY
from #temp_Temporal right join #temp_calendar on date = Fecha
group by Fecha , [Responsibility Center]
) select Fecha
, sum(Vendidas_CMEX) Vendidas_CMEX, sum(Vendidas_CMTY) Vendidas_CMTY
, sum(Instaladas_CMEX) Instaladas_CMEX , sum(Instaladas_CMTY) Instaladas_CMTY
, Sum(Total_Vendidas) Total_Vendidas, sum(Total_Instaladas) Total_Instaladas
From Transp
group by Fecha
order by Fecha asc
drop table #temp_calendar
drop table #temp_Temporal