I suggest gen'ing the MOVE clauses directly from @tables, ignoring the count. Just concatenate all the results into a RESTORE statement, and run that statement dynamically. For example:
...existing code...
DECLARE @sql nvarchar(max)
DECLARE @db_name_to_restore sysname
DECLARE @db_with_clauses nvarchar(max)
SET @db_name_to_restore = 'adventureworks2012'
SELECT @db_with_clauses = STUFF((
SELECT
', MOVE ''' + LogicalName + ''' TO ''' + PhysicalName + ''''
FROM @Table
ORDER BY FileId
FOR XML PATH('')
), 1, 2, '')
--SELECT @db_with_clauses
SET @sql =
'RESTORE DATABASE [' + @db_name_to_restore + '] ' +
'FROM DISK = ''' + @Path + ''' ' +
'WITH REPLACE, STATS = 10, ' +
@db_with_clauses
PRINT @sql
--EXEC(@sql)
SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.