Here is a better example of how to restore the indexes :
If Exists (select distinct
SchemaOwner
,TableName
,AllColName
from tblDBAIdxMetadt
where IsClustered = 1
and UniqueType 'PRIMARY KEY'
)
begin
declare @IndexName varchar(255)
,@SchemaOwner varchar (255)
,@TableName varchar (255)
,@ColumnList varchar (1000)
,@Sqlcmd varchar (2000)
set @IndexName = ?
set @SchemaOwner = ?
set @TableName = ?
set @ColumnList = ?
select @sqlcmd = 'CREATE CLUSTERED INDEX '+ltrim(rtrim(@IndexName))+' ON '+ltrim(rtrim(@SchemaOwner))+'.'+ltrim(rtrim(@TableName))+' ( '+ltrim(rtrim(@ColumnList))+' ) WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
'
exec (@sqlcmd)
end
You can used a cursor or while loop to generated or execute the scripts by filling the information with the ? marks.