Files
sql-scripts/TPDT-268 - ACP in task sequence/dba_storedProcedures/Wait_Random_Time_Sunday.sql
2024-03-07 16:52:14 +01:00

183 lines
5.5 KiB
Transact-SQL

USE [HCITools]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Wait_Random_Time_Sunday]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Wait_Random_Time_Sunday]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Wait_Random_Time_Sunday]
@in_debug tinyint = 0,
@in_Minutes smallint
AS
/*=============================================================================
Explication du traitement realise par la SP
-------------------------------------------
La SP sert à attendre un temps aléatoire compris entre 0 et une valeur définie seulement pour les pharmacies qui ne sont pas ouvertes le dimanche et pour les centrales
Contexte d'utilisation
----------------------
Appelé depuis n'importe quel job
Parametres
----------
@in_debug : non utilisé
@in_Minutes : temps maximum en minute
Creation : 15.08.19 / SPE
Modifications : 29.10.19 / SPE : Ajout de la partie centrales
21.11.19 / SPE : Ajout verification du jour si dimanche et ne pas faire de random sur les centrales GC
=============================================================================*/
set nocount on;
/*------------------- Declaration des variables --------------------*/
DECLARE @errno int,
@cvCurrentOrganizationalUnit int,
@subsidiary_id int,
@delay int,
@errmsg varchar(255),
@out_default_value varchar(60),
@format varchar(60),
@ou varchar(3),
@time datetime
/*------------ Affectation des parametres aux variables ------------*/
/*-------------------------- Traitement ---------------------------*/
BEGIN TRY
/* only if it's sunday */
IF DATEPART(DW, GETDATE()) = 1
BEGIN
IF EXISTS(SELECT 1 FROM [master].[cfg].[InstanceContext] WHERE Business = 'TPPHAR')
BEGIN
/* ------------------------------------------------------------------------------------------------------------------------------------- */
/* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 2 : RETRIEVE FORMAT AND OU CODE /////////////////////////////////////////////////// */
/* ------------------------------------------------------------------------------------------------------------------------------------- */
/* Get the cvCurrentOrganizationalUnit */
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 = @out_default_value OUTPUT,
@out_param_int_1 = null;
SELECT @cvCurrentOrganizationalUnit = convert(int,@out_default_value);
/* Check if we have a value, if not leave this SP */
IF @cvCurrentOrganizationalUnit is null
BEGIN
SELECT @errno = 70001,
@errmsg = '(APS) Error cvCurrentOrganizationalUnit does not exist!';
goto error_99;
END
/* Get the subsidiary id and OU code */
SELECT @subsidiary_id = ou.OU_subsidiary, @ou = ou.OU_Code
FROM arizona.dbo.Organizational_unit ou with (nolock)
WHERE ou.Organizational_unit_ID = @cvCurrentOrganizationalUnit;
/* Check if we have a value, if not leave this SP */
IF @subsidiary_id is null
BEGIN
SELECT @errno = 70001,
@errmsg = '(APS) Error subsidiary_id does not exist!';
goto error_99;
END
/* Get the current format */
SELECT @format = sub.SUB_code
FROM arizona.dbo.Subsidiary sub with (nolock)
WHERE sub.Subsidiary_ID = @subsidiary_id;
/* Check if we have a value, if not leave this SP */
IF @format is null
BEGIN
SELECT @errno = 70001,
@errmsg = '(APS) Error format does not exist!';
goto error_99;
END
/* Change the value into a compatible format */
IF @format = 'COOP'
BEGIN
SET @format = 'CVI'
END
IF @format = 'CENT'
BEGIN
SET @format = 'SUN'
END
IF @format = '000'
BEGIN
SET @format = 'AAI'
END
IF NOT EXISTS(SELECT 1 FROM HCITools.dbo.HCI_PARAMS WHERE HCIP_Key = 'WorkonSUN' and HCIP_value like '%' + @format+@ou + '%')
BEGIN
SELECT @delay = rand()* 60 * @in_Minutes
SELECT @time = dateadd(ss,@delay, '01.01.1900')
WAITFOR DELAY @time
END
END
ELSE
BEGIN /* Others servers (centrals) */
IF EXISTS(SELECT 1 FROM [master].[cfg].[Identity] WHERE Format = 'GCM') AND EXISTS(SELECT 1 FROM [master].[cfg].[InstanceContext] WHERE Business = 'TPCENT' and [Type] = 'PROD')
BEGIN
return(0)
END
ELSE
BEGIN
SELECT @delay = rand()* 60 * @in_Minutes
SELECT @time = dateadd(ss,@delay, '01.01.1900')
WAITFOR DELAY @time
END
END
END
/*---------------------- Traitement des erreurs ----------------------*/
END TRY
BEGIN CATCH
SELECT @errno = 70003,
@errmsg = 'error on Wait_Random_Time_Sunday!'
goto error_99
END CATCH;
/*------------------ Retour au programme appelant -----------------*/
RETURN(@@error);
/*---------------------- Traitement des erreurs ----------------------*/
error_99:
RAISERROR (@errmsg, 16, 1);
RETURN(@errno);
GO