SQL Server - Cómo eliminar transacciones antiguas por fecha de una base de datos en servidores propios para enviar a Finnegans

Consulta:

¿Cómo eliminar transacciones hasta una fecha determinada?


Respuesta:

Para eliminar transacciones hasta una determinada fecha ejecutar el script correspondiente en una base de datos de prueba y hacer un backup.
Para realizar esta tarea se debe utilizar Microsoft SQL Server Management Studio

Nota: Este artículo es exclusivamente para personal técnico.

Para realizar los siguientes pasos se necesitan conocimientos de administración de SQL Server. También se debe contar con los permisos para acceder a las bases y poder realizar modificaciones.

Al no ser productos comercializados por Finnegans, todo lo relacionado a seguridad lo debes resolver con el responsable de administración del servidor. En este caso, dejamos a disposición la documentación oficial del proveedor del producto.

Por favor, no ejecutar el procedimiento si se tienen dudas y comunicarse con Finnegans.

Pasos a seguir:

  1. Crear una nueva base de datos de prueba. Luego realizar un backup de la base de datos de producción y restaurarlo en la base de datos creada.

Para hacerlo seguir los pasos de la documentación oficial de Microsoft.

  1. Ejecutar el script realizando los siguientes pasos:
  • Abrir SQL Server Management Studio
    SSMS

  • Seleccionar Nueva Consulta
    newQuery

  • Seleccionar en el combo la nueva base de datos creada
    SelectBD

  • Copiar el siguiente script y pegarlo en la pestaña abierta en el SQL Server Management Studio

declare @Fecha datetime = 'aaaa/mm/dd'
declare @sql varchar(8000)

IF exists (select 1 from sys.tables where name = 'bsacontratoventagranoslimites')
	begin
		delete from bsacontratoventagranoslimites
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end 

IF exists (select 1 from sys.tables where name = 'bsacontratoventagranos')
	begin  
		delete from bsacontratoventagranos
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsamovimientohaciendainsumo')
	begin
		delete from bsamovimientohaciendainsumo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSAOperacionGastoCamion')
	begin
		delete from BSAOperacionGastoCamion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsaplanificacionagricolaitem')
	begin
		delete from bsaplanificacionagricolaitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPlanMovimientoStock')
	begin
		delete from BSPlanMovimientoStock
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsalaboreoitem')
	begin
		delete from bsalaboreoitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'bsalaboreotrabajo')
	begin
		delete from bsalaboreotrabajo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsalaboreoinsumo')
	begin
		delete from bsalaboreoinsumo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'bsalaboreocuadrilla')
	begin
		delete from bsalaboreocuadrilla
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsalaboreodistribucion')
	begin
		delete from bsalaboreodistribucion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsalaboreo')
	begin
		delete from bsalaboreo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bstransaccionnumeroserie')
	begin
		delete from bstransaccionnumeroserie
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmovimientostockvalorizado')
	begin
		delete from bsmovimientostockvalorizado
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmovimientostockfifo')
	begin
		delete from bsmovimientostockfifo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSAbsorcionCostosItemDocumentoDestino')
	begin
		delete from BSAbsorcionCostosItemDocumentoDestino
		where MovimientoStockID in (select MovimientoStockID from bsmovimientostock
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSAbsorcionCostosResultado')
	begin
		delete from BSAbsorcionCostosResultado where MovimientoStockID in 
		(select MovimientoStockID from bsmovimientostock where transaccionid in 
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSMovimientoStockValorizado')
	begin
		delete from BSMovimientoStockValorizado where MovimientoStockID 
		in (select MovimientoStockID from bsmovimientostock
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bsmovimientostock')
	begin
		delete from bsmovimientostock
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsasientoitemcancelacion')
	begin
		delete from bsasientoitemcancelacion
		where asientoitemidorigen in (select asientoitemid from bsasientoitem where transaccionid in 
							(select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from bsasientoitemcancelacion
		where asientoitemiddestino in (select asientoitemid from bsasientoitem where transaccionid in 
						(select transaccionid from bstransaccion where Fecha < @Fecha))
	end
	
IF exists (select 1 from sys.tables where name = 'bsasientoitemestadodatanet')
	begin
		delete from bsasientoitemestadodatanet
		where asientoitemestadoid in 
		(select asientoitemestadoid from bsasientoitemestado where asientoitemid in 
		(select asientoitemid from bsasientoitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'bsasientoitemestado')
	begin
		delete from bsasientoitemestado
		where asientoitemid in 
		(select asientoitemid from bsasientoitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end
	
IF exists (select 1 from sys.tables where name = 'BSAbsorcionCostosItemDocumentoOrigen')
	begin
		delete from BSAbsorcionCostosItemDocumentoOrigen 
		where asientoitemid in 
		(select asientoitemid from bsasientoitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSAbsorcionCostosResultado')
	begin
		delete from BSAbsorcionCostosResultado where AsientoItemID in 
		(select AsientoItemID from bsasientoitem where transaccionid in 
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end
	
IF exists (select 1 from sys.tables where name = 'bsasientoitem')
	begin
		delete from bsasientoitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'bsasientoitempresupuestado')
	begin
		delete from bsasientoitempresupuestado
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bstransacciondimension')
	begin
		delete from bstransacciondimension
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'SJLiquidacionLegajoConcepto')
	begin
		delete from SJLiquidacionLegajoConcepto
		where ParteTrabajoItemID in (select ParteTrabajoItemID from bspartetrabajoitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bspartetrabajoitem')
	begin
		delete from bspartetrabajoitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'bspermisoembarqueitemDocumentoAsociado')
	begin
		delete from bspermisoembarqueitemDocumentoAsociado
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bspermisoembarqueitemDIT')
	begin
		delete from bspermisoembarqueitemDIT
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'bspermisoembarqueitemPosicionArancelaria')
	begin
		delete from bspermisoembarqueitemPosicionArancelaria
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bspermisoembarqueitemoperacion')
	begin
		delete from bspermisoembarqueitemoperacion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPermisoembarque')
	begin
		delete from BSPermisoembarque
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSAContratoFeedlotCategoria')
	begin
		delete from BSAContratoFeedlotCategoria
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSCotizacionProducto')
	begin
		delete from BSCotizacionProducto where OperacionItemID in (select OperacionItemID from bsoperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		delete from BSOperacionItemCancelacion where OperacionItemIDDestino in (select OperacionItemID from bsoperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from BSOperacionItemCancelacion where OperacionItemIDOrigen in(select OperacionItemID from bsoperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItemLogRegla')
	begin
		delete from BSOperacionItemLogRegla  where OperacionItemID in (select OperacionItemID from bsoperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end
	
IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		delete BSOperacionItemCancelacion where OperacionItemIDOrigen in (select OperacionItemID from BSOperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	
		delete BSOperacionItemCancelacion where OperacionItemIDOrigen in (select OperacionItemID from BSOperacionitem where partidaid 
		in (select partidaid from bspartida where TransaccionID 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)))

		delete BSOperacionItemCancelacion where OperacionItemIDOrigen in (select OperacionItemID from BSOperacionitem where partidaid2 
		in (select partidaid from bspartida where TransaccionID 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)))

		delete BSOperacionItemCancelacion where OperacionItemIDDestino in (select OperacionItemID from BSOperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	
		delete BSOperacionItemCancelacion where OperacionItemIDDestino in (select OperacionItemID from BSOperacionitem where partidaid 
		in (select partidaid from bspartida where TransaccionID 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)))

		delete BSOperacionItemCancelacion where OperacionItemIDDestino in (select OperacionItemID from BSOperacionitem where partidaid2 
		in (select partidaid from bspartida where TransaccionID 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarqueItemOperacion')
	begin
		delete BSPermisoEmbarqueItemOperacion where OperacionItemID in (select OperacionItemID from BSOperacionitem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	
		delete BSPermisoEmbarqueItemOperacion where OperacionItemID in (select OperacionItemID from BSOperacionitem where partidaid 
		in (select partidaid from bspartida where TransaccionID 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)))

		delete BSPermisoEmbarqueItemOperacion where OperacionItemID in (select OperacionItemID from BSOperacionitem where partidaid2 
		in (select partidaid from bspartida where TransaccionID 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionitem')
	begin
		delete BSOperacionitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	
		delete BSOperacionitem where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))

		delete BSOperacionitem where partidaid2 in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bsmovimientostockvalorizado')
	begin
		delete from bsmovimientostockvalorizado
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSMovimientoStockValorizado')
	begin
		delete from BSMovimientoStockValorizado where MovimientoStockID in (select MovimientoStockID from BSMovimientoStock
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha)))
	end
	
IF exists (select 1 from sys.tables where name = 'BSMovimientoStockfifo')
	begin
		delete from BSMovimientoStockfifo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)

		delete from BSMovimientoStockfifo where MovimientoStockID in (select MovimientoStockID from BSMovimientoStock
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'bsmovimientostock')
	begin
		delete from bsmovimientostock
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		delete from BSOperacionItemCancelacion
		where OperacionItemIDOrigen in (select OperacionItemID from BSOperacionItem
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		delete from BSOperacionItemCancelacion
		where OperacionItemIDDestino in (select OperacionItemID from BSOperacionItem
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarqueItemOperacion')
	begin
		delete from BSPermisoEmbarqueItemOperacion
		where OperacionItemID in (select OperacionItemID from BSOperacionItem
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha)))
	end
	
IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'USRMedicionVariable')
	begin
		delete from USRMedicionVariable where MedicionID in (select MedicionID from USRMedicion
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'USRMedicion')
	begin
		delete from USRMedicion
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSALaboreoItem')
	begin
		delete from BSALaboreoItem
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSALaboreoInsumo')
	begin
		delete from BSALaboreoInsumo
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSAMovimientoHaciendaInsumo')
	begin
		delete from BSAMovimientoHaciendaInsumo
		where partidaid in (select partidaid from bspartida where TransaccionID in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bspartida')
	begin
		delete from bspartida
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end
	
IF exists (select 1 from sys.tables where name = 'bsmedicionitem')
	begin
		delete from bsmedicionitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmedicion')
	begin
		delete from bsmedicion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bspresupuestoplan')
	begin
		delete from bspresupuestoplan
		where transaccionidpresupuesto in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAPlanificacionGanaderaItem')
	begin
		delete from BSAPlanificacionGanaderaItem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsplancotizacionitem')
	begin
		delete from bsplancotizacionitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsplaningresosegresositem')
	begin
		delete from bsplaningresosegresositem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPlanificacionProduccionItem')
	begin
		delete from BSPlanificacionProduccionItem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPlanComprasVentasItem')
	begin
		delete from BSPlanComprasVentasItem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSPresupuestoPlan')
	begin
		delete from BSPresupuestoPlan where TransaccionIDPlan in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'FAFQuehacer')
	begin
		delete from FAFQuehacer where ProyectoItemID in (select ProyectoItemID from TPProyectoItem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha) )
	end

IF exists (select 1 from sys.tables where name = 'TPProyectoItemCosto')
	begin
		delete TPProyectoItemCosto where ProyectoItemID in (select ProyectoItemID from TPProyectoItem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha) ) 
	end

IF exists (select 1 from sys.tables where name = 'TPProyectoItem')
	begin
		delete from TPProyectoItem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end

IF exists (select 1 from sys.tables where name = 'BSAPlanificacionAgricolaItem')
	begin
		delete from BSAPlanificacionAgricolaItem where TransaccionID in (select TransaccionID from BSPlan where PlanID_ModeloAgricola 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSPlan')
	begin
		delete BSPlan where PlanID_ModeloAgricola in (select transaccionid from bstransaccion where Fecha < @Fecha)
		delete from bsplan
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bscotizacionproducto')
	begin
		delete from bscotizacionproducto
		where operacionitemid in (
		select operacionitemid from bsoperacionitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bsasiento')
	begin
		delete from bsasiento
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsoperacionitemcancelacion')
	begin
		delete from bsoperacionitemcancelacion 
		where 
		(operacionitemidorigen in (
		select operacionitemid from bsoperacionitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)))
		or
		(operacionitemiddestino in (
		select operacionitemid from bsoperacionitem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)))
	end
	
IF exists (select 1 from sys.tables where name = 'bsoperacionitem')
	begin
		delete from bsoperacionitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAContratoGranosEscalera')
	begin
		delete from BSAContratoGranosEscalera where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsoperacion')
	begin
		delete from bsoperacion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bspresupuesto')
	begin
		delete from bspresupuesto
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsprocesovalorizacionfifo')
	begin
		delete from bsprocesovalorizacionfifo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsprocesovalorizacion')
	begin
		delete from bsprocesovalorizacion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsordenproduccion')
	begin
		delete from bsordenproduccion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bstransaccioncotizacion')
	begin
		delete from bstransaccioncotizacion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsprocesoamortizacionbiendeuso')
	begin
		delete from bsprocesoamortizacionbiendeuso
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSValorizacionStockErrores')
	begin
		delete from BSValorizacionStockErrores where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
		delete from bsproceso
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bstransaccionretencion')
	begin
		delete from bstransaccionretencion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSDocumentoFisicoEstadoManual')
	begin
		delete from BSDocumentoFisicoEstadoManual where DocumentoFisicoID in 
		(select DocumentoFisicoID from BSDocumentoFisico  where transaccionid in
		(select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bsasientoitemestado')
	begin
		delete from bsasientoitemestado
		where asientoitemid in 
		(select asientoitemid from bsasientoitem where DocumentoFisicoID in (select DocumentoFisicoID from bsdocumentofisico 
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'bsasientoitem')
	begin
		delete from bsasientoitem
		where DocumentoFisicoID in (select DocumentoFisicoID from bsdocumentofisico 
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'bsdocumentofisico')
	begin
		delete from bsdocumentofisico
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsoperaciontesoreria')
	begin
		delete from bsoperaciontesoreria
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmantenimientotarea')
	begin
		delete from bsmantenimientotarea
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmantenimientoinsumo')
	begin
		delete from bsmantenimientoinsumo
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'bsmantenimientopersona')
	begin
		delete  from bsmantenimientopersona
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmantenimiento')
	begin
		delete from bsmantenimiento
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bstransaccionvinculacion')
	begin
		delete from bstransaccionvinculacion
		where 
		transaccionidorigen in (select transaccionid from bstransaccion where Fecha < @Fecha)
		or
		transaccioniddestino in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsoperacionmultiple')
	begin
		delete from bsoperacionmultiple
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bssalidacajaitem')
	begin
		delete from bssalidacajaitem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPresupuesto')
	begin
		delete from BSPresupuesto where EjercicioID in (select EjercicioID from bsejercicio	where TransaccionIDAperturaPatrimMonPrincipal 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from BSPresupuesto where EjercicioID in (select EjercicioID from bsejercicio	where TransaccionIDCierrePatrimMonPrincipal 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from BSPresupuesto where EjercicioID in (select EjercicioID from bsejercicio	where TransaccionIDCierreResultMonPrincipal 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from BSPresupuesto where EjercicioID in (select EjercicioID from bsejercicio	where TransaccionIDAperturaPatrimMonSecundaria 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from BSPresupuesto where EjercicioID in (select EjercicioID from bsejercicio	where TransaccionIDCierrePatrimMonSecundaria 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

		delete from BSPresupuesto where EjercicioID in (select EjercicioID from bsejercicio	where TransaccionIDCierreResultMonSecundaria 
		in (select transaccionid from bstransaccion where Fecha < @Fecha))

	end
	
IF exists (select 1 from sys.tables where name = 'bsejercicio')
	begin
		delete from bsejercicio
		where TransaccionIDAperturaPatrimMonPrincipal in (select transaccionid from bstransaccion where Fecha < @Fecha)

		delete from bsejercicio
		where TransaccionIDCierrePatrimMonPrincipal in (select transaccionid from bstransaccion where Fecha < @Fecha)

		delete from bsejercicio
		where TransaccionIDCierreResultMonPrincipal in (select transaccionid from bstransaccion where Fecha < @Fecha)

		delete from bsejercicio
		where TransaccionIDAperturaPatrimMonSecundaria in (select transaccionid from bstransaccion where Fecha < @Fecha)

		delete from bsejercicio
		where TransaccionIDCierrePatrimMonSecundaria in (select transaccionid from bstransaccion where Fecha < @Fecha)

		delete from bsejercicio
		where TransaccionIDCierreResultMonSecundaria in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsordenproduccionitem')
	begin
		delete from bsordenproduccionitem 
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsordenproduccion')
	begin
		delete from bsordenproduccion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSHojaRuta')
	begin
		delete from BSHojaRuta
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAProgramaAlimentacionLote')
	begin
		delete from BSAProgramaAlimentacionLote
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAProgramaAlimentacion')
	begin
		delete from BSAProgramaAlimentacion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'bsmedicion')
	begin
		delete from bsmedicion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPlanificacionObraItemMes')
	begin
		delete from BSPlanificacionObraItemMes
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSPlanificacionObraItemCosto')
	begin
		delete from BSPlanificacionObraItemCosto
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSPlanificacionObraPrecioInsumos')
	begin
		delete from BSPlanificacionObraPrecioInsumos
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSMedicionTransaccion')
	begin
		delete from BSMedicionTransaccion
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAContratoServicioAcopioItem')
	begin
		delete from BSAContratoServicioAcopioItem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAContratoServicioAcopioItem')
	begin
		delete from BSAContratoServicioAcopioItem
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSATrasladoGranosItemCosecha')
	begin
		delete from BSATrasladoGranosItemCosecha
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAContratoServicioAcopio')
	begin
		delete from BSAContratoServicioAcopio
		where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSTransaccionWSLog')
	begin
		delete from BSTransaccionWSLog where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end

IF exists (select 1 from sys.tables where name = 'SJLiquidacionLegajoConcepto')
	begin
		delete from SJLiquidacionLegajoConcepto where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end

IF exists (select 1 from sys.tables where name = 'SJLiquidacionLegajoConcepto')
	begin
		delete from SJLiquidacionLegajoConcepto where ParteTrabajoItemID in (select ParteTrabajoItemID from BSParteTrabajoItem where transaccionid 
		in (select transaccionid from bstransaccion where Fecha < @Fecha)) 
	end

IF exists (select 1 from sys.tables where name = 'BSParteTrabajoItem')
	begin
		delete from BSParteTrabajoItem where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end

IF exists (select 1 from sys.tables where name = 'SJLiquidacion')
	begin
		delete from SJLiquidacion where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end

IF exists (select 1 from sys.tables where name = 'BSParteTrabajoItem')
	begin
		if exists(select 1 from sys.columns where  object_id =  object_id('BSParteTrabajoItem') and name = 'LiquidacionLegajoID')
			begin				
				set @sql = 'delete from BSParteTrabajoItem where LiquidacionLegajoID 
							in (select LiquidacionLegajoID from SJLiquidacionLegajo where transaccionid 
							in (select transaccionid from bstransaccion where Fecha < '''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end
	end

IF exists (select 1 from sys.tables where name = 'SJLiquidacionLegajo')
	begin
		delete from SJLiquidacionLegajo where transaccionid in (select transaccionid from bstransaccion where Fecha < @Fecha) 
	end
	
IF exists (select 1 from sys.tables where name = 'tpcasotransaccionasociada')
	begin
		delete from tpcasotransaccionasociada where  transaccionidasociada in ( select TransaccionIDAsociada from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'TPCasoActividadSerializedData')
	begin
		delete from TPCasoActividadSerializedData where CasoActividadID in (select CasoActividadID from TPCasoActividad where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'TPCasoHistorial')
	begin
		delete from TPCasoHistorial where CasoActividadID in (select CasoActividadID from TPCasoActividad where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'TPCasoActividad')
	begin
		delete from TPCasoActividad where TransaccionID in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'tpcasohistorial')
	begin
		delete from tpcasohistorial where TransaccionID in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'tpcaso')
	begin
		delete from tpcaso where TransaccionID in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionExportacion')
	begin
		delete from BSOperacionExportacion where TransaccionID in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAAsignacionCuposVenta')
	begin
		delete from BSAAsignacionCuposVenta where TransaccionIDContratoGranos in 
		(select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSDeclaracionJurada')
	begin
		delete from BSDeclaracionJurada where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSAMovimientoHaciendaItemDTE')
	begin
		delete from BSAMovimientoHaciendaItemDTE where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem where OperacionItemID in
		(select OperacionItemID from BSOperacionItem where TransaccionID in
		(select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionitem where TransaccionID in 
		(select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSTransaccionDimension')
	begin
		delete from BSTransaccionDimension where TransaccionID in 
		(select TransaccionID from BSTransaccion where TransaccionIDProceso in 
		(select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSTransaccionCotizacion')
	begin
		delete from BSTransaccionCotizacion where TransaccionID in 
		(select TransaccionID from BSTransaccion where TransaccionIDProceso in 
		(select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSAsientoItemEstado')
	begin
		delete from BSAsientoItemEstado where AsientoItemID in (select AsientoItemID from BSAsientoitem where TransaccionID 
		in (select TransaccionID from BSTransaccion where TransaccionIDProceso 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'BSAsientoitem')
	begin
		delete from BSAsientoitem where TransaccionID in 
		(select TransaccionID from BSTransaccion where TransaccionIDProceso 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSAsiento')
	begin
		delete from BSAsiento where TransaccionID in 
		(select TransaccionID from BSTransaccion where TransaccionIDProceso 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'BSTransaccion')
	begin
		delete from BSTransaccion where TransaccionIDProceso in 
		(select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSTransaccionCotizacion')
	begin
		delete from BSTransaccionCotizacion where TransaccionID in 
		(select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		 delete from BSOperacionItemCancelacion where OperacionItemIDDestino in 
		 (select OperacionItemID from BSOperacionItem  where TransaccionID in 
		 (select TransaccionID from BSTransaccion  where TransaccionIDPadre in
		 ( select TransaccionID from BSTransaccion where Fecha < @Fecha)))

		 delete from BSOperacionItemCancelacion where OperacionItemIDOrigen in 
		 (select OperacionItemID from BSOperacionItem  where TransaccionID in 
		 (select TransaccionID from BSTransaccion  where TransaccionIDPadre in
		 ( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end
	
 IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem  where TransaccionID in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre in
		(select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem where TransaccionID in (select TransaccionID from BSOperacion where TransaccionID 
		in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

 IF exists (select 1 from sys.tables where name = 'BSOperacion')
	begin
		delete from BSOperacion where TransaccionID in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
		
		if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacion') 
							and name = 'USR_SolicitudTaller')
			begin				
				set @sql = 'delete from BSOperacion where USR_SolicitudTaller 
							in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
							in (select TransaccionID from BSTransaccion where Fecha <'''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end
	end

 IF exists (select 1 from sys.tables where name = 'TPCasoTransaccionAsociada')
	begin
		delete from TPCasoTransaccionAsociada where TransaccionIDAsociada in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre in
		(select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'TPCasoHistorial')
	begin
		delete from TPCasoHistorial where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'TPCasoTransaccionAsociada')
	begin
		 delete from TPCasoTransaccionAsociada where CasoActividadID in (select CasoActividadID from TPCasoActividad  where TransaccionID 
		 in  (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		 in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

 IF exists (select 1 from sys.tables where name = 'TPCasoActividad')
	begin
		delete from TPCasoActividad  where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end
	
 IF exists (select 1 from sys.tables where name = 'TPCaso')
	begin
		delete from TPCaso where TransaccionID in  (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'BSTransaccionCotizacion')
	begin
		delete from BSTransaccionCotizacion where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'BSTransaccionDimension')
	begin
		delete from BSTransaccionDimension where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSMovimientoStockValorizado')
	begin
		delete BSMovimientoStockValorizado where MovimientoStockID in 
		(select MovimientoStockID from BSMovimientoStock where TransaccionID 
		in  (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)) )
	end

  IF exists (select 1 from sys.tables where name = 'BSMovimientoStock')
	begin
		delete from BSMovimientoStock where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)) 
	end

	
  IF exists (select 1 from sys.tables where name = 'BSAsientoItemEstado')
	begin
		delete from BSAsientoItemEstado where AsientoItemID in (select AsientoItemID from BSAsientoItem where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSAsientoItem')
	begin
		delete from BSAsientoItem where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))

	end

  IF exists (select 1 from sys.tables where name = 'BSAsiento')
	begin
		delete from BSAsiento where TransaccionID in  
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSTransaccionDimension')
	begin
		delete from BSTransaccionDimension where TransaccionID in 
		(select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end
	
  IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		delete from BSOperacionItemCancelacion where OperacionItemIDOrigen in 
		(select OperacionItemID from BSOperacionItem where TransaccionID 
		in (select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))))

		delete from BSOperacionItemCancelacion where OperacionItemIDDestino in 
		(select OperacionItemID from BSOperacionItem where TransaccionID 
		in (select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))))
	end

  IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem where TransaccionID in 
		(select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSOperacion')
	begin
		delete from BSOperacion where TransaccionID in 
		(select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSTransaccionCotizacion')
	begin
		delete from BSTransaccionCotizacion where TransaccionID in 
		(select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSAsientoItem')
	begin
		delete BSAsientoItem where TransaccionID in 
		(select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSAsiento')
	begin
		delete from BSAsiento where TransaccionID in 
		(select TransaccionID  from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'TPCasoHistorial')
	begin
		delete from TPCasoHistorial  where TransaccionID in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'TPCasoActividad')
	begin
		delete from TPCasoActividad  where TransaccionID in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'TPCaso')
	begin
		delete TPCaso where TransaccionID in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end


	 update BSTransaccion set TransaccionIDPadre = null 
	 where TransaccionIDPadre in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
	 in( select TransaccionID from BSTransaccion where Fecha < @Fecha))


  IF exists (select 1 from sys.tables where name = 'FAFQuehacer')
	begin
		delete from FAFQuehacer where ProyectoItemID in 
		(select ProyectoItemID from TPProyectoItem  where TransaccionID 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'TPProyectoItemCosto')
	begin
		delete from TPProyectoItemCosto where ProyectoItemID in 
		(select ProyectoItemID from TPProyectoItem  where TransaccionID 
		in(select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'TPProyectoItem')
	begin
		delete from TPProyectoItem  where TransaccionID in
		(select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSAPlanificacionAgricolaItem')
	begin
		delete BSAPlanificacionAgricolaItem where TransaccionID in 
		(select TransaccionID from BSPlan where PlanID_ModeloAgricola 
		in (select TransaccionID  from BSPlan  where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

 IF exists (select 1 from sys.tables where name = 'BSPlan')
	begin
		delete from BSPlan where PlanID_ModeloAgricola in (select TransaccionID  from BSPlan  where TransaccionID 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'TPProyectoItem')
	begin
		delete from TPProyectoItem where TransaccionID in( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

  IF exists (select 1 from sys.tables where name = 'BSPresupuestoPlan')
	begin
		delete from BSPresupuestoPlan where TransaccionIDPlan in( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

  IF exists (select 1 from sys.tables where name = 'BSPlan')
	begin
		delete from BSPlan  where TransaccionID in( select TransaccionID from BSTransaccion where Fecha < @Fecha) 
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoItem')
	begin
		delete from BSALaboreoItem where LaboreoTrabajoID in (select LaboreoTrabajoID from BSALaboreoTrabajo where TransaccionID 
		in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoTrabajo')
	begin
		delete from BSALaboreoTrabajo where TransaccionID in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end 

  IF exists (select 1 from sys.tables where name = 'BSALaboreoItem')
	begin
		delete from BSALaboreoItem where TransaccionID in 
		(select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end
 
  IF exists (select 1 from sys.tables where name = 'BSALaboreoInsumo')
	begin
		delete from BSALaboreoInsumo  where TransaccionID in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoDistribucion')
	begin
		delete from BSALaboreoDistribucion  where TransaccionID in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoCuadrilla')
	begin
		delete from BSALaboreoCuadrilla where TransaccionID in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreo')
	begin
		delete from BSALaboreo where TransaccionID in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSParteTrabajoItem')
	begin
		delete from BSParteTrabajoItem where TransaccionID in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end
	
  delete from BSTransaccion where TransaccionIDPadre  in (select TransaccionID from BSTransaccion  where TransaccionIDPadre 
  in( select TransaccionID from BSTransaccion where Fecha < @Fecha))

    IF exists (select 1 from sys.tables where name = 'USRMedicionVariable')
	begin
		delete from USRMedicionVariable where MedicionID in (select MedicionID from USRMedicion where PartidaID 
		in (select PartidaID  from BSPartida where TransaccionID 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))))
	end

  IF exists (select 1 from sys.tables where name = 'USRMedicion')
	begin
		delete from USRMedicion where PartidaID in (select PartidaID  from BSPartida where TransaccionID 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

    IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem where PartidaID 
		in (select PartidaID from BSPartida where TransaccionID 
		in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSPartida')
	begin
		delete from BSPartida where TransaccionID in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSAMovimientoHaciendaItemDTE')
	begin
		delete from BSAMovimientoHaciendaItemDTE where TransaccionID in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSPartida')
	begin
		delete from BSPartida where TransaccionID in( select TransaccionID from BSTransaccion  where TransaccionIDPadre 
		in( select TransaccionID from BSTransaccion where Fecha < @Fecha))

		delete from BSPartida where TransaccionID in(select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end
  
  update BSTransaccion set TransaccionIDPadre = null where TransaccionIDPadre in (select TransaccionID from BSTransaccion where Fecha < @Fecha)

  delete from BSTransaccion  where TransaccionIDPadre in( select TransaccionID from BSTransaccion where Fecha < @Fecha) 

  IF exists (select 1 from sys.tables where name = 'BSAbsorcionCostos')
	begin
		delete from BSAbsorcionCostos where TransaccionID in( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

  IF exists (select 1 from sys.tables where name = 'BSAVariacionBiomasaItem')
	begin
		delete from BSAVariacionBiomasaItem where TransaccionID in( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

  IF exists (select 1 from sys.tables where name = 'TPParteActividadMasiva')
	begin
		delete from TPParteActividadMasiva where TransaccionID in( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoItem')
	begin
		delete from BSALaboreoItem where LaboreoTrabajoID in 
		(select LaboreoTrabajoID from BSALaboreoTrabajo where TransaccionID 
		in( select TransaccionID from BSALaboreo where TransaccionIDOrdenTrabajo 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoTrabajo')
	begin
		delete from BSALaboreoTrabajo where TransaccionID in( select TransaccionID from BSALaboreo where TransaccionIDOrdenTrabajo 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end
  
  IF exists (select 1 from sys.tables where name = 'BSALaboreoItem')
	begin
		delete from BSALaboreoItem where TransaccionID in( select TransaccionID from BSALaboreo where TransaccionIDOrdenTrabajo 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoInsumo')
	begin
		delete from BSALaboreoInsumo where TransaccionID in( select TransaccionID from BSALaboreo where TransaccionIDOrdenTrabajo 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoDistribucion')
	begin
		delete from BSALaboreoDistribucion where TransaccionID in( select TransaccionID from BSALaboreo where TransaccionIDOrdenTrabajo 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreoCuadrilla')
	begin
		delete from BSALaboreoCuadrilla where TransaccionID in( select TransaccionID from BSALaboreo where TransaccionIDOrdenTrabajo 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

  IF exists (select 1 from sys.tables where name = 'BSALaboreo')
	begin
		delete from BSALaboreo where TransaccionIDOrdenTrabajo in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end
	
  IF exists (select 1 from sys.tables where name = 'BSTransaccionRetencion')
	begin
		delete from BSTransaccionRetencion where TransaccionIDOrigen in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

  IF exists (select 1 from sys.tables where name = 'BSPlan')
	begin
		delete from BSPlan where PlanMarcoID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
		delete from BSPlan where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSTransaccionNumeroSerie')
	begin
		delete from BSTransaccionNumeroSerie where NumeroSerieID in 
		(select NumeroSerieID from BSNumeroSerie where TransaccionIDOrigen in 
		( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end
 
  IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem where NumeroSerieID2 in ( select NumeroSerieID from BSNumeroSerie where TransaccionIDOrigen 
		in ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

 IF exists (select 1 from sys.tables where name = 'BSNumeroSerie')
	begin
		delete from BSNumeroSerie where TransaccionIDOrigen in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSParteTrabajoItem')
	begin
		delete from BSParteTrabajoItem where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'USR_DeclaracionJuradaOpExpo')
	begin
		delete from USR_DeclaracionJuradaOpExpo where TransaccionIDOpExpo in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'USRItemGastoExportacion')
	begin
		delete from USRItemGastoExportacion where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarqueItemOperacion')
	begin
		delete from BSPermisoEmbarqueItemOperacion where  TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)

		if exists(select 1 from sys.columns where  object_id =  object_id('BSPermisoEmbarque') 
					and name = 'USR_TransaccionIDOperacionExpo')
			begin				
				set @sql = 'delete from BSPermisoEmbarqueItemOperacion where  TransaccionID 
							in (select TransaccionID from BSPermisoEmbarque where USR_TransaccionIDOperacionExpo 
							in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end
	end


 IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarqueItemDocumentoAsociado')
	begin
		delete from BSPermisoEmbarqueItemDocumentoAsociado where  TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)

		if exists(select 1 from sys.columns where  object_id =  object_id('BSPermisoEmbarque') 
					and name = 'USR_TransaccionIDOperacionExpo')
			begin				
				set @sql = 'delete from BSPermisoEmbarqueItemDocumentoAsociado where  TransaccionID 
							in (select TransaccionID from BSPermisoEmbarque where USR_TransaccionIDOperacionExpo 
							in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end
	end

 IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarque')
	begin
		if exists(select 1 from sys.columns where  object_id =  object_id('BSPermisoEmbarque') 
					and name = 'USR_TransaccionIDOperacionExpo')
			begin				
				set @sql = 'delete from BSPermisoEmbarque where USR_TransaccionIDOperacionExpo 
							in ( select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end

		delete from BSPermisoEmbarque where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'USR_CobranzaAnticipoContrato')
	begin
		delete from USR_CobranzaAnticipoContrato where TransaccionIDContrato in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarqueItemDeclaracionJurada')
	begin
		delete from BSPermisoEmbarqueItemDeclaracionJurada where TransaccionIDDeclaracionJurada in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSPermisoEmbarqueItemSeguimientoBuque')
	begin
		delete from BSPermisoEmbarqueItemSeguimientoBuque where TransaccionIDPermiso in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSAProgramaAlimentacionGanaderaTurno')
	begin
		delete from BSAProgramaAlimentacionGanaderaTurno where TransaccionIDRonda in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'BSARondaReparto')
	begin
		delete from BSARondaReparto where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'USRLaboreoRemito')
	begin
		delete from USRLaboreoRemito where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

 IF exists (select 1 from sys.tables where name = 'USRLaboreoRemito')
	begin
		delete from USRLaboreoRemito where TransaccionID in ( select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end 

IF exists (select 1 from sys.tables where name = 'BSOperacionItemCancelacion')
	begin
		 delete from BSOperacionItemCancelacion where OperacionItemIDDestino in 
		 (select OperacionItemID from BSOperacionItem  where TransaccionID in 
		 ( select TransaccionID from BSTransaccion where Fecha < @Fecha))

		 delete from BSOperacionItemCancelacion where OperacionItemIDOrigen in 
		 (select OperacionItemID from BSOperacionItem  where TransaccionID in 
		 ( select TransaccionID from BSTransaccion where Fecha < @Fecha))
		 		 
		 if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacion') 
							and name = 'USR_SolicitudTaller')
			begin				
				set @sql = 'delete from BSOperacionItemCancelacion where OperacionItemIDDestino 
							in (select OperacionItemID from BSOperacionItem where TransaccionID 
							in(select TransaccionID from BSOperacion where USR_SolicitudTaller 
							in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+''')))
				
							delete from BSOperacionItemCancelacion where OperacionItemIDOrigen 
							in (select OperacionItemID from BSOperacionItem where TransaccionID 
							in(select TransaccionID from BSOperacion where USR_SolicitudTaller 
							in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+''')))'
				exec @sql
			end

		if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacionItemCancelacion') 
							and name = 'USR_SolicitudTaller')
			begin				
				set @sql = 'delete from BSOperacionItemCancelacion where OperacionItemIDDestino 
							in (select OperacionItemID from BSOperacionItem where TransaccionID 
							in(select TransaccionID from BSOperacion where USR_TransaccionIDLiqEntrega 
							in (select TransaccionID from BSTransaccion where Fecha <'''+convert(varchar,@Fecha)+''')))
				
							delete from BSOperacionItemCancelacion where OperacionItemIDOrigen 
							in (select OperacionItemID from BSOperacionItem where TransaccionID 
							in(select TransaccionID from BSOperacion where USR_TransaccionIDLiqEntrega 
							in (select TransaccionID from BSTransaccion where Fecha <'''+convert(varchar,@Fecha)+''')))'
				exec @sql
			end

		delete from BSOperacionItemCancelacion where OperacionItemIDDestino in 
		 (select OperacionItemID from BSOperacionItem where TransaccionID 
		 in(select TransaccionID from BSOperacion where TransaccionIDContratoGranos 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)))

		 delete from BSOperacionItemCancelacion where OperacionItemIDOrigen in 
		 (select OperacionItemID from BSOperacionItem where TransaccionID 
		 in(select TransaccionID from BSOperacion where TransaccionIDContratoGranos 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)))
	end

IF exists (select 1 from sys.tables where name = 'BSOperacionItem')
	begin
		delete from BSOperacionItem where TransaccionID in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
		
		if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacionItem') 
							and name = 'USR_SolicitudTaller')
			begin				
				set @sql = 'delete from BSOperacionItem where TransaccionID 
							in (select TransaccionID from BSOperacion where USR_SolicitudTaller 
							in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end

		if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacionItem') 
							and name = 'USR_TransaccionIDLiqEntrega')
			begin				
				set @sql = 'delete from BSOperacionItem where TransaccionID 
						in(select TransaccionID from BSOperacion where USR_TransaccionIDLiqEntrega 
						in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+'''))'
				exec @sql
			end

		delete from BSOperacionItem where TransaccionID in(select TransaccionID from BSOperacion where TransaccionIDContratoGranos 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha))

	end

IF exists (select 1 from sys.tables where name = 'BSOperacion')
	begin
		delete from BSOperacion where TransaccionID in (select TransaccionID from BSTransaccion where Fecha < @Fecha)

		if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacion') 
							and name = 'USR_SolicitudTaller')
			begin				
				set @sql = 'delete from BSOperacion where USR_SolicitudTaller 
				in (select TransaccionID from BSTransaccion where Fecha < '''+convert(varchar,@Fecha)+''')'
				exec @sql
			end
		
		if exists(select 1 from sys.columns where  object_id =  object_id('BSOperacion') 
							and name = 'USR_TransaccionIDLiqEntrega')
			begin				
				set @sql = 'delete from BSOperacion where USR_TransaccionIDLiqEntrega 
						in (select TransaccionID from BSTransaccion where Fecha <'''+convert(varchar,@Fecha)+''')'
				exec @sql
			end

		delete from BSOperacion where TransaccionIDContratoGranos in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end


IF exists (select 1 from sys.tables where name = 'USR_DeclaracionJuradaOpExpo')
	begin
		delete from USR_DeclaracionJuradaOpExpo where TransaccionIDDeclaracionJurada 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'USR_CobranzaAnticipoContrato')
	begin
		delete from USR_CobranzaAnticipoContrato where TransaccionIDCobranza 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSATrasladoGranosDepositoOrigen')
	begin
		delete from BSATrasladoGranosDepositoOrigen where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end
	

IF exists (select 1 from sys.tables where name = 'USRMedicionVariable')
	begin
		delete from USRMedicionVariable where MedicionID in (select MedicionID from USRMedicion where USR_RemitoIDOrigen 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'USRMedicion')
	begin
		delete from USRMedicion where USR_RemitoIDOrigen 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'RHMovimientoVacaciones')
	begin
		delete from RHMovimientoVacaciones where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end	

IF exists (select 1 from sys.tables where name = 'RHConsumoVacacionesItem')
	begin
		delete from RHConsumoVacacionesItem where ConsumoVacacionesID in (select ConsumoVacacionesID from RHConsumoVacaciones where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha))
	end

IF exists (select 1 from sys.tables where name = 'RHConsumoVacaciones')
	begin
		delete from RHConsumoVacaciones where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end
	
IF exists (select 1 from sys.tables where name = 'BSLegajoImportacionDocumentoAsociado')
	begin
		delete from BSLegajoImportacionDocumentoAsociado where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSLegajoImportacion')
	begin
		delete from BSLegajoImportacion where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'SJEntregaMateriales')
	begin
		delete from SJEntregaMateriales where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'SJEntregaMaterialesLegajo')
	begin
		delete from SJEntregaMaterialesLegajo where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

IF exists (select 1 from sys.tables where name = 'BSMovimientoStock')
	begin
		delete from BSMovimientoStock where TransaccionID 
		in (select TransaccionID from BSTransaccion where Fecha < @Fecha)
	end

delete from BSTransaccion where Fecha < @Fecha

Reemplazar aaaa/mm/dd por la fecha hasta la que se quiere eliminar las transacciones. Por ejemplo ‘2021/12/01’ eliminará todas las transacciones anteriores al 01 de diciembre de 2021.

  • Ejecutar el script
    Execute por Fecha
  1. Realizar un backup de la base de datos de prueba creada y enviar el archivo generado mediante el FTP de FInnegans, utilizando las credenciales que le han sido entregadas por Finnegans.
1 me gusta