title: How to fix "Logical file X is not part of database Y..." while all names are correct
body: <p>When restoring a backup, I have an error:</p>
  
  <blockquote>
    <p>Logical file 'foo' is not part of database 'YY'. Use RESTORE
    FILELISTONLY to list the logical file names.</p>
  </blockquote>
  
  <p>The restore script work fine if I do <code>WITH FILE = 1</code> but it shows the error, if I do <code>WITH FILE = 2</code>.</p>
  
  <p>I have run FILELISTONLY and name "foo" is really there and its correct.</p>
  
  <p>I have run HEADERONLY and there are two items, and I need second one restored.</p>
  
  <p>Actual script:</p>
  
  <pre><code>USE [master]
  RESTORE DATABASE [ni_5] 
  FROM  DISK = N'C:\backup\ni_5.bak' WITH  FILE = 2 /* will work if i put 1 here*/,   NOUNLOAD,  STATS = 10,
  MOVE N'foo_dat' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ni_5.mdf',  
  MOVE N'Fooind1' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_1.ndf',  
  MOVE N'Fooind10' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_10.ndf',  
  MOVE N'Fooind11' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_11.ndf', 
  MOVE N'Fooind12' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_12.ndf',  
  MOVE N'Fooind2' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_2.ndf',  
  MOVE N'Fooind3' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_3.ndf',  
  MOVE N'Fooind4' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_4.ndf',  
  MOVE N'Fooind5' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_5.ndf',  
  MOVE N'Fooind6' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_6.ndf',  
  MOVE N'Fooind7' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_7.ndf',  
  MOVE N'Fooind8' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_8.ndf',  
  MOVE N'Fooind9' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_9.ndf',  
  MOVE N'foo_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\foo_13.ldf'
  
  
  GO
  </code></pre>
  
  <p>Actual output:</p>
  
  <pre><code>Msg 3154, Level 16, State 4, Line 2
  The backup set holds a backup of a database other than the existing 'ni_5' database.
  Msg 3013, Level 16, State 1, Line 2
  RESTORE DATABASE is terminating abnormally.
  </code></pre>