Notes
Notes - notes.io |
DECLARE @FechaInicio DATETIME, @FechaFin DATETIME;
DECLARE @FechaDummy DATETIME = '1900-01-01'; -- Fecha dummy por defecto
BEGIN TRY
BEGIN TRANSACTION;
-- Obtener el rango de fechas de la tabla origen, ignorando valores NULL
SELECT @MinFecha = MIN(FechaColumna), @MaxFecha = MAX(FechaColumna)
FROM dbo.TablaOrigen
WHERE FechaColumna IS NOT NULL;
-- Validar que la tabla origen tenga fechas válidas
IF @MinFecha IS NULL OR @MaxFecha IS NULL
BEGIN
PRINT 'La tabla origen no contiene fechas válidas.';
ROLLBACK TRANSACTION;
RETURN;
END;
PRINT 'Iniciando transferencia de datos...';
-- Inicializar variables para la transferencia por intervalos
SET @FechaInicio = @MinFecha;
SET @FechaFin = DATEADD(MONTH, 3, @FechaInicio);
WHILE @FechaInicio <= @MaxFecha
BEGIN
PRINT 'Transfiriendo datos del intervalo: '
+ CONVERT(VARCHAR, @FechaInicio, 120) + ' a '
+ CONVERT(VARCHAR, @FechaFin, 120);
-- Transferir datos del intervalo actual
INSERT INTO dbo.TablaDestino (Columna1, Columna2, FechaColumna, ...)
SELECT Columna1, Columna2, ISNULL(FechaColumna, @FechaDummy) AS FechaColumna, ...
FROM dbo.TablaOrigen
WHERE (FechaColumna >= @FechaInicio AND FechaColumna < @FechaFin)
OR FechaColumna IS NULL;
-- Actualizar el rango de fechas para el siguiente intervalo
SET @FechaInicio = @FechaFin;
SET @FechaFin = DATEADD(MONTH, 3, @FechaInicio);
END;
PRINT 'Transferencia completada exitosamente.';
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
PRINT 'Ocurrió un error: ' + ERROR_MESSAGE();
ROLLBACK TRANSACTION;
END CATCH;
DECLARE @MinFecha DATETIME, @MaxFecha DATETIME;
DECLARE @FechaInicio DATETIME, @FechaFin DATETIME;
DECLARE @FechaDummy DATETIME = '1900-01-01'; -- Fecha dummy por defecto
BEGIN TRY
BEGIN TRANSACTION;
-- Obtener el rango de fechas de la tabla origen, ignorando valores NULL
SELECT @MinFecha = MIN(FechaColumna), @MaxFecha = MAX(FechaColumna)
FROM dbo.TablaOrigen
WHERE FechaColumna IS NOT NULL;
-- Validar que la tabla origen tenga fechas válidas
IF @MinFecha IS NULL OR @MaxFecha IS NULL
BEGIN
PRINT 'La tabla origen no contiene fechas válidas.';
ROLLBACK TRANSACTION;
RETURN;
END;
PRINT 'Iniciando transferencia de datos...';
-- Inicializar variables para la transferencia por intervalos
SET @FechaInicio = @MinFecha;
SET @FechaFin = DATEADD(MONTH, 3, @FechaInicio);
WHILE @FechaInicio <= @MaxFecha
BEGIN
PRINT 'Transfiriendo datos del intervalo: '
+ CONVERT(VARCHAR, @FechaInicio, 120) + ' a '
+ CONVERT(VARCHAR, @FechaFin, 120);
-- Activar IDENTITY_INSERT para la tabla destino
SET IDENTITY_INSERT dbo.TablaDestino ON;
-- Transferir datos del intervalo actual
INSERT INTO dbo.TablaDestino (ColumnaIdentity, Columna1, Columna2, FechaColumna, ...)
SELECT ColumnaIdentity, Columna1, Columna2, ISNULL(FechaColumna, @FechaDummy) AS FechaColumna, ...
FROM dbo.TablaOrigen
WHERE (FechaColumna >= @FechaInicio AND FechaColumna < @FechaFin)
OR FechaColumna IS NULL;
-- Desactivar IDENTITY_INSERT para la tabla destino
SET IDENTITY_INSERT dbo.TablaDestino OFF;
-- Actualizar el rango de fechas para el siguiente intervalo
SET @FechaInicio = @FechaFin;
SET @FechaFin = DATEADD(MONTH, 3, @FechaInicio);
END;
PRINT 'Transferencia completada exitosamente.';
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
PRINT 'Ocurrió un error: ' + ERROR_MESSAGE();
ROLLBACK TRANSACTION;
END CATCH;
![]() |
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
