SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE SQDR.DISABLEINDEXES
	@tschema nvarchar(128) ,
	@tname nvarchar(128)
	
AS
BEGIN

DECLARE @my_sql2 NVARCHAR(200);
DECLARE @sql nvarchar(500);

   set @sql =  'DECLARE cur_disable CURSOR FOR select ''ALTER INDEX '' + I.name + '' ON '' + S.name + ''.'' + T.name + '' DISABLE'' from sys.indexes I inner join sys.tables T on I.object_id = T.object_id 
   inner join sys.schemas S on T.schema_id = S.schema_id 
   where I.type_desc = ''NONCLUSTERED'' and I.name is not null and T.name=''' + N'@tname' + ''' and S.name= ''' + N'@tschema' + ''';'

EXEC sp_executesql @sql
   
OPEN cur_disable;
FETCH NEXT FROM cur_disable INTO @my_sql2;
WHILE @@FETCH_STATUS = 0
   BEGIN
      EXECUTE sp_executesql  @my_sql2;
      FETCH NEXT FROM cur_disable INTO @my_sql2;
   END;
CLOSE cur_disable;
DEALLOCATE cur_disable;

END

