

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS where Table_Name='equ_alquiler' and column_name = 'id_documento' )
begin
BEGIN TRANSACTION AGREGADOC

	DECLARE @restriccion NVARCHAR(200)
	DECLARE @comando NVARCHAR (1000)
	SELECT @restriccion = NAME FROM sys.objects WHERE parent_object_id in (SELECT object_id FROM sys.objects WHERE NAME like 'equ_alquiler') and NAME like 'DF__equ_alqui__id_do__%'
	IF exists (SELECT * FROM sys.objects WHERE NAME=@restriccion)
	BEGIN
		--Deshabilitar restricción
		SET @comando = N'ALTER TABLE [equ_alquiler] DROP CONSTRAINT ' + @restriccion
		EXECUTE sp_executesql @comando
	END	
	
	ALTER TABLE equ_alquiler ADD
		id_documento int NOT NULL CONSTRAINT DF_equ_alquiler_id_documento DEFAULT 0
	
	ALTER TABLE dbo.equ_alquiler ADD CONSTRAINT
	FK_equ_alquiler_doc_documento FOREIGN KEY
	(
	id_documento
	) REFERENCES dbo.doc_documento
	(
	id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	 
	--ALTER TABLE dbo.equ_alquiler SET (LOCK_ESCALATION = TABLE)	
COMMIT TRANSACTION AGREGADOC

END

GO


	
if exists(select * FROM INFORMATION_SCHEMA.COLUMNS where Table_Name='equ_alquiler' and column_name = 'id_documento.ot' )
begin

	BEGIN TRANSACTION QUITAOT
	
	exec ('update equ_alquiler set id_documento=[id_documento.ot]')

	ALTER TABLE equ_alquiler
	DROP CONSTRAINT FK_equ_alquiler_id_documento_ot
	
	ALTER TABLE dbo.equ_alquiler
	DROP COLUMN [id_documento.ot]
	
	COMMIT TRANSACTION QUITAOT
	
end
