358 lines
24 KiB
Transact-SQL
358 lines
24 KiB
Transact-SQL
/* Drop existing standard schedule for job */
|
||
declare @schedule_id int
|
||
declare c_schedules cursor local forward_only static for
|
||
select ss.schedule_id
|
||
from msdb.dbo.sysjobschedules sjs
|
||
INNER JOIN msdb.dbo.sysschedules ss
|
||
ON sjs.schedule_id = ss.schedule_id
|
||
AND ss.name NOT LIKE '%#SPEC#'
|
||
INNER JOIN msdb.dbo.sysjobs sj
|
||
ON sjs.job_id = sj.job_id
|
||
WHERE sj.name = N'_D00414 - LORE, mise à jour des articles'
|
||
|
||
open c_schedules
|
||
|
||
FETCH NEXT FROM c_schedules into @schedule_id
|
||
while @@fetch_status = 0
|
||
begin
|
||
IF ((select COUNT(*) from msdb.dbo.sysjobschedules where schedule_id=@schedule_id) = 1)
|
||
EXEC msdb.dbo.sp_delete_schedule @schedule_id=@schedule_id, @force_delete = 1
|
||
FETCH NEXT FROM c_schedules into @schedule_id
|
||
end
|
||
|
||
close c_schedules
|
||
|
||
deallocate c_schedules
|
||
|
||
IF EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'_D00414 - LORE, mise à jour des articles')
|
||
EXEC msdb.dbo.sp_delete_job @job_name = N'_D00414 - LORE, mise à jour des articles', @delete_unused_schedule=0
|
||
GO
|
||
|
||
/* Creation Job and Steps*/
|
||
BEGIN TRANSACTION
|
||
DECLARE @ReturnCode INT
|
||
SELECT @ReturnCode = 0
|
||
|
||
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'TPH-Business' AND category_class=1)
|
||
BEGIN
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'TPH-Business'
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
|
||
END
|
||
|
||
/* Add Job */
|
||
DECLARE @jobId BINARY(16)
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'_D00414 - LORE, mise à jour des articles',
|
||
@enabled=1,
|
||
@notify_level_eventlog=0,
|
||
@notify_level_email=0,
|
||
@notify_level_netsend=0,
|
||
@notify_level_page=0,
|
||
@delete_level=0,
|
||
@description=N'Traitement des articles de la liste LORE
|
||
----------------------------------------------------------------
|
||
08.04.2013 - PDE - Ajout test de CV, pour que ce job existe aussi dans les pharmacies tessinoises
|
||
13.05.2013 - PDE - Traitement qui tourne aussi dans les pharmacies tessinoises
|
||
06.01.2021 - FLA - Add step Article Remplacement
|
||
22.01.2021 - FLA - Modify code in Article Remplacement
|
||
17.03.2022 - FLA : Change DBA mail
|
||
03.02.2023 - SPE : Add step',
|
||
@category_name=N'TPH-Business',
|
||
@start_step_id=1,
|
||
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
|
||
/* Add Step */
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Empty step',
|
||
@step_id=1,
|
||
@cmdexec_success_code=0,
|
||
@on_success_action=3,
|
||
@on_success_step_id=0,
|
||
@on_fail_action=3,
|
||
@on_fail_step_id=0,
|
||
@retry_attempts=0,
|
||
@retry_interval=0,
|
||
@os_run_priority=0, @subsystem=N'TSQL',
|
||
@command=N'/* Empty step */',
|
||
@database_name=N'master',
|
||
@output_file_name=NULL,
|
||
@flags=0,
|
||
@database_user_name=NULL,
|
||
@server=NULL,
|
||
@additional_parameters=NULL,
|
||
@proxy_id=NULL,
|
||
@proxy_name=NULL
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
/* Add Step */
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Traitement des articles LORE',
|
||
@step_id=2,
|
||
@cmdexec_success_code=0,
|
||
@on_success_action=3,
|
||
@on_success_step_id=0,
|
||
@on_fail_action=4,
|
||
@on_fail_step_id=6,
|
||
@retry_attempts=0,
|
||
@retry_interval=0,
|
||
@os_run_priority=0, @subsystem=N'TSQL',
|
||
@command=N'declare @msg varchar(255);
|
||
|
||
exec aps_LORE_Item_Status @out_errmsg = @msg output;
|
||
',
|
||
@database_name=N'Arizona',
|
||
@output_file_name=NULL,
|
||
@flags=0,
|
||
@database_user_name=NULL,
|
||
@server=NULL,
|
||
@additional_parameters=NULL,
|
||
@proxy_id=NULL,
|
||
@proxy_name=NULL
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
/* Add Step */
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Article Remplacement - Reprise MIN/Max pour PLC B01 - A02',
|
||
@step_id=3,
|
||
@cmdexec_success_code=0,
|
||
@on_success_action=3,
|
||
@on_success_step_id=0,
|
||
@on_fail_action=4,
|
||
@on_fail_step_id=6,
|
||
@retry_attempts=0,
|
||
@retry_interval=0,
|
||
@os_run_priority=0, @subsystem=N'TSQL',
|
||
@command=N'/* Creation des ITSSI MIN/Max
|
||
de l’ancien article sur le nouveau dans le cas ou le nouveau n’en a pas encore et que l’ancien enm a toujours
|
||
|
||
*/
|
||
use arizona
|
||
|
||
select ITL_linked_item into ##xx from item_link ITL
|
||
|
||
where ITL.ITL_type = 5
|
||
and itl.ITL_start_date <= getdate()
|
||
and isnull(itl.ITL_end_date,''2099-12-31'') >= getdate()
|
||
-- and itl_linked_item = 19196488
|
||
group by ITL_linked_item
|
||
|
||
having count(*) > 1
|
||
|
||
declare @cvCurrentOrganizationalUnit int,
|
||
@cvPHGDInventoryType int,
|
||
@value_date date,
|
||
@Criteria_type_id int,
|
||
@subsidiary_id int
|
||
|
||
/*------------ Affectation des parametres aux variables --------------*/
|
||
|
||
/*----------------- Recherche de l''OU par défaut ------------------------*/
|
||
select @cvCurrentOrganizationalUnit = NULL
|
||
exec arizona.dbo.sp_bmc_Bmc_Applic_Default
|
||
@in_job_type = 3,
|
||
@in_param_int_1 = NULL,
|
||
@in_param_int_2 = NULL,
|
||
@in_param_varchar_1 = ''cvCurrentOrganizationalUnit'',
|
||
@out_default_value = @cvCurrentOrganizationalUnit output,
|
||
@out_param_int_1 = null
|
||
|
||
|
||
/* @SUBsidiary_id */
|
||
select @subsidiary_id = null
|
||
select @subsidiary_id = OU.ou_subsidiary
|
||
from Arizona.dbo.Organizational_unit OU with (nolock)
|
||
where OU.Organizational_unit_ID = @cvCurrentOrganizationalUnit
|
||
|
||
select @cvPHGDInventoryType = Null
|
||
exec Arizona.dbo.sp_bmc_Bmc_Applic_Default
|
||
@in_job_type = 3,
|
||
@in_param_int_1 = NULL,
|
||
@in_param_int_2 = @Subsidiary_id,
|
||
@in_param_varchar_1 = ''cvPHGDInventoryType'',
|
||
@out_default_value = @cvPHGDInventoryType output,
|
||
@out_param_int_1 = null
|
||
|
||
/* Criteria_type_id */
|
||
select @Criteria_type_id = null
|
||
select @Criteria_type_id = CT.criteria_type_id
|
||
from Arizona.dbo.criteria_type CT
|
||
where CT.CRT_code = ''plc''
|
||
and CT.CRT_type = 2 /* Article */
|
||
|
||
select @value_date = convert(varchar(10), getdate(), 120)
|
||
|
||
insert into Arizona.dbo.Item_seasonal_stock_info
|
||
(
|
||
Item_seasonal_stock_info_GUID,
|
||
ITSSI_item,
|
||
ITSSI_inventory_type,
|
||
ITSSI_order_point_qty,
|
||
ITSSI_optimal_inventory_qty,
|
||
ITSSI_value_date
|
||
)
|
||
select
|
||
NEWID(),
|
||
ITK1.ITK_item,
|
||
@cvPHGDInventoryType,
|
||
ITSSI.ITSSI_order_point_qty,
|
||
ITSSI.ITSSI_optimal_inventory_qty,
|
||
@value_date
|
||
|
||
|
||
from Arizona.dbo.item_key itk -- Old_item
|
||
|
||
join Arizona.dbo.item_link ITL
|
||
on itl.ITL_item = itk.itk_item
|
||
and itl.ITL_type = 5
|
||
and itl.ITL_start_date <= getdate()
|
||
and isnull(itl.ITL_end_date,''2099-12-31'') >= getdate()
|
||
|
||
left outer join ##xx xx
|
||
on xx.ITL_linked_item = itl.ITL_linked_item
|
||
|
||
join Arizona.dbo.Item_seasonal_stock_info ITSSI with (nolock)
|
||
on ITSSI.itssi_item = itk.itk_item
|
||
and (ITSSI.ITSSI_order_point_qty is not null
|
||
or ITSSI.ITSSI_optimal_inventory_qty is not null)
|
||
and itssi.ITSSI_value_date < getdate()
|
||
and itssi.Item_seasonal_stock_info_GUID = (select top 1 xx.item_seasonal_stock_info_guid from Arizona.dbo.Item_seasonal_stock_info XX with (nolock)
|
||
where XX.itssi_item = itk.itk_item
|
||
and (XX.ITSSI_order_point_qty is not null
|
||
or XX.ITSSI_optimal_inventory_qty is not null)
|
||
and XX.ITSSI_value_date < getdate()
|
||
order by XX.ITSSI_value_date desc)
|
||
|
||
join Arizona.dbo.item_key ITK1 -- New_item
|
||
on itk1.itk_item = itl.ITL_linked_item
|
||
and itk1.itk_type = 1
|
||
and itk1.itk_subsidiary = @subsidiary_id
|
||
|
||
join Arizona.dbo.item_criteria ITCR
|
||
on itcr.itcr_item = itk.itk_item
|
||
|
||
join Arizona.dbo.criteria CRIT
|
||
on crit.CR_criteria_type = @Criteria_type_id
|
||
and crit.criteria_id = itcr.ITCR_criteria
|
||
|
||
LEFT OUTER join Arizona.dbo.Item_seasonal_stock_info ITSSI1 with (nolock)
|
||
on ITSSI1.itssi_item = itk1.itk_item
|
||
|
||
where itk.ITK_type = 1
|
||
and itk.ITK_subsidiary = @subsidiary_id
|
||
and CRIT.cr_code in (''B01'', ''A02'')
|
||
AND itssi1.Item_seasonal_stock_info_GUID IS null
|
||
and xx.ITL_linked_item is null
|
||
|
||
drop table ##xx
|
||
',
|
||
@database_name=N'Arizona',
|
||
@output_file_name=NULL,
|
||
@flags=0,
|
||
@database_user_name=NULL,
|
||
@server=NULL,
|
||
@additional_parameters=NULL,
|
||
@proxy_id=NULL,
|
||
@proxy_name=NULL
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
/* Add Step */
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Modify Min Max to 0',
|
||
@step_id=4,
|
||
@cmdexec_success_code=0,
|
||
@on_success_action=3,
|
||
@on_success_step_id=0,
|
||
@on_fail_action=4,
|
||
@on_fail_step_id=6,
|
||
@retry_attempts=0,
|
||
@retry_interval=0,
|
||
@os_run_priority=0, @subsystem=N'TSQL',
|
||
@command=N'exec dbo.aps_Manage_Item_Leaving_LORE_Item_Facing',
|
||
@database_name=N'Arizona',
|
||
@output_file_name=NULL,
|
||
@flags=0,
|
||
@database_user_name=NULL,
|
||
@server=NULL,
|
||
@additional_parameters=NULL,
|
||
@proxy_id=NULL,
|
||
@proxy_name=NULL
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
/* Add Step */
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Empty step for success',
|
||
@step_id=5,
|
||
@cmdexec_success_code=0,
|
||
@on_success_action=1,
|
||
@on_success_step_id=0,
|
||
@on_fail_action=4,
|
||
@on_fail_step_id=6,
|
||
@retry_attempts=0,
|
||
@retry_interval=0,
|
||
@os_run_priority=0, @subsystem=N'TSQL',
|
||
@command=N'/* Empty step */
|
||
exec Get_Job_Error_Info @in_JobName = ''_D00414 - LORE, mise à jour des articles'', @in_Recipients = ''DBA_operator''',
|
||
@database_name=N'HCITools',
|
||
@output_file_name=NULL,
|
||
@flags=0,
|
||
@database_user_name=NULL,
|
||
@server=NULL,
|
||
@additional_parameters=NULL,
|
||
@proxy_id=NULL,
|
||
@proxy_name=NULL
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
/* Add Step */
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Send email KO',
|
||
@step_id=6,
|
||
@cmdexec_success_code=0,
|
||
@on_success_action=2,
|
||
@on_success_step_id=0,
|
||
@on_fail_action=2,
|
||
@on_fail_step_id=0,
|
||
@retry_attempts=0,
|
||
@retry_interval=0,
|
||
@os_run_priority=0, @subsystem=N'TSQL',
|
||
@command=N'exec Get_Job_Error_Info @in_JobName = ''_D00414 - LORE, mise à jour des articles'', @in_Recipients = ''DBA_operator''',
|
||
@database_name=N'HCITools',
|
||
@output_file_name=NULL,
|
||
@flags=0,
|
||
@database_user_name=NULL,
|
||
@server=NULL,
|
||
@additional_parameters=NULL,
|
||
@proxy_id=NULL,
|
||
@proxy_name=NULL
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
|
||
/* Attach existing specific schedule for job */
|
||
declare @enabled_schedule int,
|
||
@schedule_name nvarchar(50)
|
||
declare c_schedules cursor local forward_only static for
|
||
select enabled, name
|
||
from msdb.dbo.sysschedules
|
||
where name LIKE '_D00414%'
|
||
and name LIKE '%#SPEC#'
|
||
|
||
open c_schedules
|
||
|
||
FETCH NEXT FROM c_schedules into @enabled_schedule, @schedule_name
|
||
while @@fetch_status = 0
|
||
begin
|
||
EXEC @ReturnCode = msdb.dbo.sp_attach_schedule @job_id = @jobId, @schedule_name=@schedule_name
|
||
IF(@enabled_schedule = 1)
|
||
begin
|
||
SET @schedule_name = SUBSTRING(@schedule_name,0,LEN(@schedule_name)-5)
|
||
IF EXISTS (select name from msdb.dbo.sysschedules where name = @schedule_name)
|
||
EXEC @ReturnCode = msdb.dbo.sp_update_schedule @name=@schedule_name, @enabled=0
|
||
end
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
FETCH NEXT FROM c_schedules into @enabled_schedule, @schedule_name
|
||
end
|
||
|
||
close c_schedules
|
||
|
||
deallocate c_schedules
|
||
|
||
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
|
||
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
|
||
COMMIT TRANSACTION
|
||
GOTO EndSave
|
||
QuitWithRollback:
|
||
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
|
||
EndSave:
|
||
|
||
GO
|