-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6MartPeriodico].[DM_ASSETCLASS_pre_selective]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @ID_ELAB INT
	SET @ID_ELAB = [C6MartPeriodico].GETIDELAB()
	-- Log esecuzione
	INSERT INTO LOG_ESECUZIONE
		(Nome, Inizio, Nota)
	VALUES (
		'DM_ASSETCLASS_pre_selective',
		GETDATE(),
		'Elaborazione trimestrale'
	)
	TRUNCATE TABLE [C6MartPeriodico].ASSETCLASS
	INSERT INTO [C6MartPeriodico].[ASSETCLASS]
           ([ID_ASSETCLASS]
           ,[DESCRIZIONE]
           ,[LIVELLO]
           ,[ORDINAMENTO]
           ,[ID_ELAB])
	SELECT 
		LTRIM(RTRIM(ASSETCLASS.OID$)) AS ID_ASSETCLASS,
		LTRIM(RTRIM(ASSETCLASS.ASSETCLASSDESCR)) AS DESCRIZIONE,
		ASSETCLASS.ASSETCLASSLEV AS LIVELLO,
		ASSETCLASS.ORDINE AS ORDINAMENTO,
		@ID_ELAB AS ID_ELAB
	FROM C6StagingPeriodico.ASSETCLASS
	-- Log esecuzione
	UPDATE
		LOG_ESECUZIONE
	SET
		Fine = GETDATE(),
		Tipo = 'INSERT',
		Righe = @@ROWCOUNT
	WHERE
		Nome = 'DM_ASSETCLASS_pre_selective' AND
		Inizio = (
			SELECT
				MAX(Inizio)
			FROM
				LOG_ESECUZIONE
			WHERE
				Nome = 'DM_ASSETCLASS_pre_selective'
		)
END