

IF EXISTS (SELECT * FROM SYSCOLUMNS INNER JOIN SYSOBJECTS ON SYSOBJECTS.id = SYSCOLUMNS.id WHERE SYSCOLUMNS.NAME = 'EFATURAZARF' AND SYSOBJECTS.name = 'FATURA')
ALTER TABLE FATURA ALTER COLUMN EFATURAZARF NVARCHAR(36) NULL
IF EXISTS (SELECT * FROM SYSCOLUMNS INNER JOIN SYSOBJECTS ON SYSOBJECTS.id = SYSCOLUMNS.id WHERE SYSCOLUMNS.NAME = 'EFATURA_CEVAP_UUID' AND SYSOBJECTS.name = 'FATURA')
ALTER TABLE FATURA ALTER COLUMN EFATURA_CEVAP_UUID NVARCHAR(36) NULL

IF EXISTS (SELECT * FROM SYSCOLUMNS INNER JOIN SYSOBJECTS ON SYSOBJECTS.id = SYSCOLUMNS.id WHERE SYSCOLUMNS.NAME = 'UUID' AND SYSOBJECTS.name = 'FATURA')
BEGIN
    UPDATE FATURA SET UUID = newid() WHERE UUID IS NULL
    
    DECLARE @sql NVARCHAR(2000)
    WHILE 1=1
    BEGIN
	   SELECT TOP 1 @sql = N'alter table FATURA drop constraint ['+dc.NAME+N']'
	   from sys.default_constraints dc
	   JOIN sys.columns c
		  ON c.default_object_id = dc.object_id
	   WHERE 
		  dc.parent_object_id = OBJECT_ID('FATURA')
	   AND c.name = N'UUID'
	   IF @@ROWCOUNT = 0 BREAK
	   EXEC (@sql)
    END

    ALTER TABLE FATURA ALTER COLUMN UUID NVARCHAR(36) NOT NULL

    ALTER TABLE FATURA ADD  DEFAULT (newid()) FOR [UUID]
END