IF COL_LENGTH('xVentanas', 'Id_Ventana') IS NOT NULL AND COL_LENGTH('xVentanas', 'Id_Ventana') <> 255
BEGIN 

	BEGIN TRY 

	    
		-- Inicializo la transacción
		BEGIN TRAN

		IF COL_LENGTH('XVENTANAS_ITEMS', 'Texto1') IS NOT NULL  ALTER TABLE XVENTANAS_ITEMS DROP COLUMN Texto1
		IF COL_LENGTH('XVENTANAS_ITEMS_USERS', 'Texto1') IS NOT NULL  ALTER TABLE XVENTANAS_ITEMS_USERS DROP COLUMN Texto1

		
		-- XVENTANAS_ITEMS_USERS
		select * into #XVENTANAS_ITEMS_USERS from XVENTANAS_ITEMS_USERS
		delete from XVENTANAS_ITEMS_USERS
		
		drop table XVENTANAS_ITEMS_USERS
		
		-- XVENTANAS_ITEMS
		select * into #XVENTANAS_ITEMS from XVENTANAS_ITEMS
		delete from XVENTANAS_ITEMS
		
		drop table XVENTANAS_ITEMS
		
		-- XVENTANAS
		select * into #xventanas from xVentanas
		delete from xVentanas
		
		drop table xVentanas
		
		CREATE TABLE [dbo].[xVentanas](
			[Id_Ventana] [varchar](255) NOT NULL,
			[Nombre] [varchar](200) NULL,
			[Fuente_Tipo] [varchar](31) NULL,
			[Fuente_Tam] [int] NULL,
			[Fuente_Color] [varchar](50) NULL,
			[Fuente_Estilo] [int] NULL,
			[Fuente_Charset] [int] NULL,
		CONSTRAINT [PK_xVN_PorId] PRIMARY KEY CLUSTERED 
		(
			[Id_Ventana] ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]
		
		
		insert into xVentanas select * from #xventanas




		
		-- INSERTAR XVENTANAS_ITEMS
		CREATE TABLE [dbo].[xVentanas_Items](
			[Id_Ventana] [varchar](255) NOT NULL,
			[Item] [int] NULL,
			[Nombre] [varchar](200) NOT NULL,
			[Tipo] [smallint] NULL,
			[Fuente_Tipo] [varchar](31) NULL,
			[Fuente_Tam] [int] NULL,
			[Fuente_Color] [varchar](50) NULL,
			[Fuente_Estilo] [int] NULL,
			[Fuente_Charset] [int] NULL,
			[Color_Texto] [varchar](50) NULL,
			[Color_Fondo] [int] NULL,
			[Pos_X] [int] NULL,
			[Pos_Y] [int] NULL,
			[Pos_Ancho] [int] NULL,
			[Pos_Alto] [int] NULL,
			[Justificacion] [char](1) NULL,
			[Valor] [varchar](5000) NULL,
			[Ocultar] [tinyint] NULL,
			[Requerido] [tinyint] NULL,
			[Flag_Modif] [tinyint] NULL,
			[Deshabilitado] [tinyint] NULL,
			[Flag_Modif_Pos] [tinyint] NULL,
			[Texto] [varchar](500) NULL,
			[Flag_Agregar] [tinyint] NULL,
			[Flag_Cambiar] [tinyint] NULL,
			[Flag_Borrar] [tinyint] NULL,
			[Flag_Ver] [tinyint] NULL,
			[Flag_Todas] [tinyint] NULL,
			[Etiqueta] [varchar](400) NULL,
		CONSTRAINT [PK_xVNI_PorId] PRIMARY KEY CLUSTERED 
		(
			[Id_Ventana] ASC,
			[Nombre] ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]
		
		
		ALTER TABLE [dbo].[xVentanas_Items]  WITH CHECK ADD  CONSTRAINT [FK_xVNI_xVentanas] FOREIGN KEY([Id_Ventana])
		REFERENCES [dbo].[xVentanas] ([Id_Ventana])
		
		
		ALTER TABLE [dbo].[xVentanas_Items] CHECK CONSTRAINT [FK_xVNI_xVentanas]
		
		
		insert into XVENTANAS_ITEMS select * from #XVENTANAS_ITEMS


		
		-- INSERTAR XVENTANAS_ITEMS_USERS
		
		CREATE TABLE [dbo].[xVentanas_Items_Users](
			[Id_Ventana] [varchar](255) NOT NULL,
			[Id_Usuario] [smallint] NOT NULL,
			[Nombre] [varchar](200) NOT NULL,
			[Item] [int] NOT NULL,
			[Tipo] [smallint] NULL,
			[Fuente_Tipo] [varchar](31) NULL,
			[Fuente_Tam] [int] NULL,
			[Fuente_Color] [varchar](50) NULL,
			[Fuente_Estilo] [int] NULL,
			[Fuente_Charset] [int] NULL,
			[Color_Texto] [varchar](50) NULL,
			[Color_Fondo] [int] NULL,
			[Pos_X] [int] NULL,
			[Pos_Y] [int] NULL,
			[Pos_Ancho] [int] NULL,
			[Pos_Alto] [int] NULL,
			[Justificacion] [char](1) NULL,
			[Valor] [varchar](5000) NULL,
			[Ocultar] [tinyint] NULL,
			[Requerido] [tinyint] NULL,
			[Flag_Modif] [tinyint] NULL,
			[Deshabilitado] [tinyint] NULL,
			[Flag_Modif_Pos] [tinyint] NULL,
			[Texto] [varchar](500) NULL,
			[Flag_Agregar] [tinyint] NULL,
			[Flag_Cambiar] [tinyint] NULL,
			[Flag_Borrar] [tinyint] NULL,
			[Flag_Ver] [tinyint] NULL,
			[Flag_Todas] [tinyint] NULL,
			[Etiqueta] [varchar](400) NULL,
		CONSTRAINT [PK_xVNIU_PorId] PRIMARY KEY CLUSTERED 
		(
			[Id_Ventana] ASC,
			[Id_Usuario] ASC,
			[Nombre] ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]
		
		ALTER TABLE [dbo].[xVentanas_Items_Users]  WITH CHECK ADD  CONSTRAINT [FK_xVNIU_xVentanas] FOREIGN KEY([Id_Ventana])
		REFERENCES [dbo].[xVentanas] ([Id_Ventana])
		
		ALTER TABLE [dbo].[xVentanas_Items_Users] CHECK CONSTRAINT [FK_xVNIU_xVentanas]
		
		insert into XVENTANAS_ITEMS_USERS select * from #XVENTANAS_ITEMS_USERS
		
		drop table #xventanas
		drop table #xventanas_items
		drop table #xventanas_items_users
		
		/*
		select *  from #xventanas
		select *  from #xventanas_items
		select *  from #xventanas_items_users
		*/

		-- Confirmo todos los cambios
		COMMIT;
	END TRY
	BEGIN CATCH
		ROLLBACK;

		-- Manejo del error
		DECLARE @ErrorMessage NVARCHAR(4000);
		DECLARE @ErrorSeverity INT;
		DECLARE @ErrorState INT;

		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

	END CATCH;
END