IF OBJECT_ID('dbo.Index_EVENT_BASE') IS NOT NULL 
		DROP TABLE dbo.Index_EVENT_BASE

---------------------------------------------------------------------------------
	CREATE TABLE dbo.Index_EVENT_BASE (
		script nvarchar(max),
		type_ VARCHAR(50)
	)
INSERT INTO dbo.Index_EVENT_BASE(script,type_)
select STUFF((
SELECT 
   'DROP INDEX [' + i.name + '] ON ' + OBJECT_SCHEMA_NAME(i.object_id) + '.' + OBJECT_NAME(t.object_id) + ';'
FROM SYS.indexes AS i
join sys.tables t on t.object_id = i.object_id
where t.name = 'T_EVENT_BASE' and OBJECT_SCHEMA_NAME(t.object_id) = 'AKUZ'
and i.name like 'IX_%' and i.type_desc = 'NONCLUSTERED'
and exists(select * 
				from SYS.index_columns AS icc 
				JOIN SYS.columns AS cc ON icc.object_id = cc.object_id AND icc.column_id = cc.column_id
				where i.object_id = icc.object_id AND i.index_id = icc.index_id
				and cc.name = 'AmbulanceCard')
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 0, '') as script, 'delete' as type_ 

INSERT INTO dbo.Index_EVENT_BASE(script,type_)
select STUFF((
SELECT 
    Replace('CREATE INDEX [' + i.name + '] ON ' + OBJECT_SCHEMA_NAME(i.object_id) + '.' + OBJECT_NAME(t.object_id) + '(' + (SELECT STUFF((
																																	SELECT 
																																	    c.name+','
																																	FROM SYS.index_columns AS ic
																																		JOIN SYS.columns AS c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
																																	where i.object_id = ic.object_id AND i.index_id = ic.index_id and ic.is_included_column = 0
																																	FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 0, ''))
																																	+ ') INCLUDE ('+  (SELECT STUFF((
																																	SELECT 
																																	    c.name+','
																																	FROM SYS.index_columns AS ic
																																		JOIN SYS.columns AS c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
																																	where i.object_id = ic.object_id AND i.index_id = ic.index_id and ic.is_included_column = 1
																																	FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 0, ''))
																																	+') WITH (FILLFACTOR=95, SORT_IN_TEMPDB=ON, MAXDOP = 0); ', ',)', ')')
FROM SYS.indexes AS i
join sys.tables t on t.object_id = i.object_id
where t.name = 'T_EVENT_BASE' and OBJECT_SCHEMA_NAME(t.object_id) = 'AKUZ'
and i.name like 'IX_%' and i.type_desc = 'NONCLUSTERED'
and exists(select * 
				from SYS.index_columns AS icc 
				JOIN SYS.columns AS cc ON icc.object_id = cc.object_id AND icc.column_id = cc.column_id
				where i.object_id = icc.object_id AND i.index_id = icc.index_id
				and cc.name = 'AmbulanceCard')
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 0, '') as script, 'insert' as type_ ;

--select * from dbo.Index_EVENT_BASE

DECLARE @index nvarchar(max);
select top 1 @index = script from dbo.Index_EVENT_BASE where type_ = 'delete'
if(@index is not null)
begin
	EXEC sp_executesql @index;
end

update eb
set eb.AmbulanceCard = acr.AmbulanceCard
from AKUZ.T_EVENT_BASE eb 
join AKUZ.T_AMBULANCE_CARD_RECORD acr on acr.AmbulanceEvent = eb.EventID
where eb.AmbulanceCard is null;

delete from AKUZ.T_EVENT_BASE 
where AmbulanceCard is null;

ALTER TABLE AKUZ.T_EVENT_BASE ALTER COLUMN AmbulanceCard uniqueidentifier NOT NULL;
GO

DECLARE @index nvarchar(max);
select top 1 @index = script from dbo.Index_EVENT_BASE where type_ = 'insert'
if(@index is not null)
begin
	EXEC sp_executesql @index;
end

IF OBJECT_ID('dbo.Index_EVENT_BASE') IS NOT NULL 
		DROP TABLE dbo.Index_EVENT_BASE
