-- Schema: dbo
-- Stored Procedure: TrimWSEITables

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [dbo].[TrimWSEITables]
	@data DECIMAL(9,0)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	DECLARE @ok BIT

	SET @ok = 0

	BEGIN TRAN

	-- WSEIAN

	SELECT *
	INTO #TEMPAN
	FROM C6StagingPeriodico.WSEIAN2
	WHERE DTTRIM = @data

	IF @@ROWCOUNT > 0
	BEGIN
		TRUNCATE TABLE C6StagingPeriodico.WSEIAN2

		INSERT INTO C6StagingPeriodico.WSEIAN2
		SELECT *
		FROM #TEMPAN

		SET @ok = 1
	END
	ELSE
		SET @ok = 0

	DROP TABLE #TEMPAN

	-- WSEICS

	SELECT *
	INTO #TEMPCS
	FROM C6StagingPeriodico.WSEICS
	WHERE DTTRIM = @data

	IF @@ROWCOUNT > 0
	BEGIN
		TRUNCATE TABLE C6StagingPeriodico.WSEICS

		INSERT INTO C6StagingPeriodico.WSEICS
		SELECT *
		FROM #TEMPCS

		SET @ok = 1
	END
	ELSE
		SET @ok = 0

	DROP TABLE #TEMPCS

	-- WSEIPV

	SELECT *
	INTO #TEMPPV
	FROM C6StagingPeriodico.WSEIPV
	WHERE DTTRIM = @data

	IF @@ROWCOUNT > 0
	BEGIN
		TRUNCATE TABLE C6StagingPeriodico.WSEIPV

		INSERT INTO C6StagingPeriodico.WSEIPV
		SELECT *
		FROM #TEMPPV

		SET @ok = 1
	END
	ELSE
		SET @ok = 0

	DROP TABLE #TEMPPV

	-- WSEIUL

	SELECT *
	INTO #TEMPUL
	FROM C6StagingPeriodico.WSEIUL
	WHERE DTTRIM = @data

	IF @@ROWCOUNT > 0
	BEGIN
		TRUNCATE TABLE C6StagingPeriodico.WSEIUL

		INSERT INTO C6StagingPeriodico.WSEIUL
		SELECT *
		FROM #TEMPUL

		SET @ok = 1
	END
	ELSE
		SET @ok = 0
	
	DROP TABLE #TEMPUL

	IF @ok = 1
		COMMIT TRAN
	ELSE
		ROLLBACK TRAN
END


