NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

-- ============================================
-- Procedimiento: usp_DetectarDuplicadosMensuales
-- Autor: [Tu Nombre]
-- Descripción: Detecta registros duplicados por mes en cualquier tabla que tenga los campos requeridos
-- ============================================

CREATE OR ALTER PROCEDURE usp_DetectarDuplicadosMensuales
@SchemaName SYSNAME,
@TableName SYSNAME,
@FechaIDCol NVARCHAR(128) = 'FechaID', -- Puede ajustarse si el campo de fecha tiene otro nombre
@Output BIT = 1 -- 1 para ver los resultados, 0 para no mostrarlos
AS
BEGIN
SET NOCOUNT ON;

DECLARE @SQL NVARCHAR(MAX) = '';
DECLARE @MinFechaID INT, @MaxFechaID INT;
DECLARE @MesActual INT, @UltimoMes INT;
DECLARE @FechaInicio INT, @SiguienteMes INT, @SiguienteMesFechaInicio INT;
DECLARE @Anno INT, @Mes INT, @Duplicados BIGINT;

-- Crear tabla temporal para resultados
IF OBJECT_ID('tempdb..#DuplicadosPorMes') IS NOT NULL
DROP TABLE #DuplicadosPorMes;

CREATE TABLE #DuplicadosPorMes (
Mes INT,
Anno INT,
TotalDuplicados BIGINT
);

-- Obtener fechas mínimas y máximas
SET @SQL = '
SELECT
@MinFechaID_OUT = MIN([' + @FechaIDCol + ']),
@MaxFechaID_OUT = MAX([' + @FechaIDCol + '])
FROM [' + @SchemaName + '].[' + @TableName + '];';

EXEC sp_executesql @SQL,
N'@MinFechaID_OUT INT OUTPUT, @MaxFechaID_OUT INT OUTPUT',
@MinFechaID_OUT = @MinFechaID OUTPUT, @MaxFechaID_OUT = @MaxFechaID OUTPUT;

-- Inicializar ciclo mensual
SET @MesActual = @MinFechaID / 100;
SET @UltimoMes = @MaxFechaID / 100;

WHILE @MesActual <= @UltimoMes
BEGIN
SET @FechaInicio = @MesActual * 100 + 1;

-- Calcular siguiente mes
SET @Anno = @MesActual / 100;
SET @Mes = @MesActual % 100;

IF @Mes = 12
BEGIN
SET @Anno = @Anno + 1;
SET @Mes = 1;
END
ELSE
SET @Mes = @Mes + 1;

SET @SiguienteMes = @Anno * 100 + @Mes;
SET @SiguienteMesFechaInicio = @SiguienteMes * 100 + 1;

-- Detectar duplicados
SET @SQL = '
SELECT @Dup_OUT = ISNULL(SUM(DupCount - 1), 0)
FROM (
SELECT COUNT(*) AS DupCount
FROM [' + @SchemaName + '].[' + @TableName + ']
WHERE [' + @FechaIDCol + '] >= ' + CAST(@FechaInicio AS NVARCHAR) + '
AND [' + @FechaIDCol + '] < ' + CAST(@SiguienteMesFechaInicio AS NVARCHAR) + '
GROUP BY FechaEvento, TransaccionID, ObjetoID, [' + @FechaIDCol + ']
HAVING COUNT(*) > 1
) AS Duplicados;';

EXEC sp_executesql @SQL,
N'@Dup_OUT BIGINT OUTPUT',
@Dup_OUT = @Duplicados OUTPUT;

-- Insertar resultado
INSERT INTO #DuplicadosPorMes (Mes, Anno, TotalDuplicados)
VALUES (@MesActual, @MesActual / 100, @Duplicados);

SET @MesActual = @SiguienteMes;
END

-- Mostrar resultados si se desea
IF @Output = 1
BEGIN
SELECT * FROM #DuplicadosPorMes;
END
END;
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.