select rso.RabbitSendObjectID, 0 as 'tt'
INTO #RABBIT
from AKUZ.T_RABBIT_SEND_OBJECT rso
where rso.OwnerID is not null and TRY_CAST(rso.OwnerID as uniqueidentifier) is null

insert into #RABBIT(RabbitSendObjectID, tt)
select rso.RabbitSendObjectID, 1
from AKUZ.T_RABBIT_SEND_OBJECT rso
where rso.UserID is not null and TRY_CAST(rso.UserID as uniqueidentifier) is null

update rso
set rso.OwnerID = null
from AKUZ.T_RABBIT_SEND_OBJECT rso
join #RABBIT r on r.RabbitSendObjectID = rso.RabbitSendObjectID and r.tt = 0

update rso
set rso.UserID = null
from AKUZ.T_RABBIT_SEND_OBJECT rso
join #RABBIT r on r.RabbitSendObjectID = rso.RabbitSendObjectID and r.tt = 1

if exists(select * from sys.indexes i where i.[object_id] = object_id('AKUZ.T_RABBIT_SEND_OBJECT', 'U') and i.name = 'IX_T_RABBIT_SEND_OBJECT_OwnerID')
	drop index IX_T_RABBIT_SEND_OBJECT_OwnerID on AKUZ.T_RABBIT_SEND_OBJECT
go
if exists(select * from sys.indexes i where i.[object_id] = object_id('AKUZ.T_RABBIT_SEND_OBJECT', 'U') and i.name = 'IX_T_RABBIT_SEND_OBJECT_Type')
	drop index IX_T_RABBIT_SEND_OBJECT_Type on AKUZ.T_RABBIT_SEND_OBJECT
go
if exists(select * from sys.indexes i where i.[object_id] = object_id('AKUZ.T_RABBIT_SEND_OBJECT', 'U') and i.name = 'IX_T_RABBIT_SEND_OBJECT_SendDateTime')
	drop index IX_T_RABBIT_SEND_OBJECT_SendDateTime on AKUZ.T_RABBIT_SEND_OBJECT
go

ALTER TABLE [AKUZ].[T_RABBIT_SEND_OBJECT] ALTER COLUMN [UserID] uniqueidentifier NULL
GO

ALTER TABLE [AKUZ].[T_RABBIT_SEND_OBJECT] ALTER COLUMN [OwnerID] uniqueidentifier NULL
GO

CREATE INDEX IX_T_RABBIT_SEND_OBJECT_OwnerID ON AKUZ.T_RABBIT_SEND_OBJECT ( [OwnerID] ) WITH (FILLFACTOR=90, SORT_IN_TEMPDB= ON);
go

drop table #RABBIT

