Weird - the code (for rebuilding all indexed in a database) works in SQL Server 2005/2008/2008R2, but NOT in SQL Server 2012 - Update: now OK on 11.0.3401

--OK, it works in SQL Server 2012 too. I test it today on 11.0.3401.

USE AdventureWorks2012
GO

DECLARE tables_cursor CURSOR
FOR
SELECT s.NAME
     ,t.NAME
FROM sys.objects AS t
INNER JOIN sys.schemas AS s ON s.schema_id = t.schema_id
WHERE t.type = 'U';

OPEN tables_cursor;

DECLARE @schemaname SYSNAME;
DECLARE @tablename SYSNAME;

FETCH NEXT
FROM tables_cursor
INTO @schemaname
     ,@tablename

WHILE (@@FETCH_STATUS <> - 1)
BEGIN
     EXECUTE ('ALTER INDEX ALL ON ' + @schemaname + '.' + @tablename + ' Rebuild')

     FETCH NEXT
     FROM tables_cursor
     INTO @schemaname
           ,@tablename;
END

PRINT 'The indexes on all tables have been rebuilt.';

CLOSE tables_cursor;

DEALLOCATE tables_cursor;


Solved - SQL Server 2012 Semantic Search Example Works Now

If you run the following sample code on BOL (http://technet.microsoft.com/en-us/library/gg492075), you will see some error messages as the sample code is not complete.
SET @Title = 'Sample Document.docx'

SELECT @DocID = DocumentID
   FROM Documents
   WHERE DocumentTitle = @Title

SELECT @Title AS Title, keyphrase, score
   FROM SEMANTICKEYPHRASETABLE(Documents, *, @DocID)
   ORDER BY score DESC
(1) @Title and @DocID are not declared.

(2) The column names DocumentID and DocumentTitle are different from ID and Title in the dbo.Documents table when we attach the sample database
(3) The Document table in the sample database does not have a row for 'Sample Document.docx'
(4) After installing the sample semantic database, we need to enable semantic search on the Documents table and column(s).

(1) Install and configure Semantic Search (http://technet.microsoft.com/en-us/library/gg509085)
(2) Enable Semantic Search on Tables and Columns (http://technet.microsoft.com/en-us/library/gg509116).

(3) Finally,modify the code to perform semantic search.

=============================CODE================================

Use SemanticsDB
GO

--Create a fulltext catalog
CREATE FULLTEXT CATALOG ft AS DEFAULT
GO
/*
1. Verify the columns and contents in the sample 'dbo.Documents' table.
2. We care about 3 columns the most: id, title, and docexcerpt. The last one will have a full-text index on it.
3. Also note there are four titles in the sample table as below:
Columnstore Indices and Batch Processing
Introduction to DataMining
Why Is Bleeding Edge a Different Conference
Additivity of Measures
*/
SELECT * FROM [dbo].[Documents]
/*
1. Create a full-text index for the column(s) for semantic search. In the sample table, that's docexcerpt
2. Notice the PK for the able (PK_Document) is used.
*/

CREATE FULLTEXT INDEX ON [dbo].[Documents]
        (docexcerpt       
            Language 1033      
            Statistical_Semantics
)
KEY INDEX PK_Documents
WITH STOPLIST = SYSTEM
GO
--Now we can perform semantic search on the docexcerpt column.
DECLARE @Title varchar(50)
DECLARE @DocID varchar(50)
SET @Title = 'Introduction to Data Mining' --notice:this is one entry in the table, also no .docx
SELECT @DocID = ID --Note: not DocumentID as in the sample code

FROM dbo.Documents
WHERE Title = @Title --Note: not DocumentTitle as in the sample code
SELECT @Title AS Title, keyphrase, score
FROM SEMANTICKEYPHRASETABLE(dbo.Documents,*, @DocID)
ORDER BY score DESC

--The output is as below:
/*
Title                                                                    keyphrase              score
===================================================
Introduction to Data Mining                               necessity           0.3286656
Introduction to Data Mining                               anymore            0.2987682
Introduction to Data Mining                               mining                0.2788366
Introduction to Data Mining                               rare              0.2755147
Introduction to Data Mining                               becoming          0.262227
Introduction to Data Mining                               advantage         0.2589051
Introduction to Data Mining                               data                    0.2256858
Introduction to Data Mining                               using                  0.2256858
Introduction to Data Mining                               every                  0.2057543
Introduction to Data Mining                               companies        0.1991104
Introduction to Data Mining                               company            0.1758569
Introduction to Data Mining                               not                      0.1293499
*/


SQL Server Error 3154: The backup set holds a backup of a database other than the existing database

Problem: 

I run the code below for playing the new Database Recovery Advisor
(from http://blogs.msdn.com/b/managingsql/archive/2011/07/13/recovery-advisor-an-introduction.aspx). As my SQL Server 2012 instance name is a named instance, I have made some changes. But I get an error message when running the restore command: Error 3154: The backup set holds a backup of a database other than the existing database  'fork_scenario'.

CREATE DATABASE fork_scenario
GO

USE fork_scenario
GO

CREATE TABLE t (c INT)
GO

--My instance name is SQL2012, so changed the default name MSSQLSERVER to SQL2012

BACKUP DATABASE [fork_scenario] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak'
WITH NOFORMAT
,NOINIT
,NAME = N'fork_scenario-Full Database Backup'
,SKIP
,NOREWIND
,NOUNLOAD
,STATS = 10
GO

INSERT INTO t
VALUES (1)

BACKUP DATABASE [fork_scenario] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak'
WITH DIFFERENTIAL
,NOFORMAT
,NOINIT
,NAME = N'fork_scenario-Differential Database Backup'
,SKIP
,NOREWIND
,NOUNLOAD
,STATS = 10
GO

INSERT INTO t
VALUES (2)

--wait for a minute
BACKUP LOG [fork_scenario] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak'
WITH NOFORMAT
,NOINIT
,NAME = N'backup_fork-Transaction Log Backup'
,SKIP
,NOREWIND
,NOUNLOAD
,STATS = 10
GO

--Recover to the differential backup.
USE [master]

ALTER DATABASE [fork_scenario]

SET SINGLE_USER
WITH

ROLLBACK IMMEDIATE
GO

RESTORE DATABASE [fork_scenario]
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak'
WITH FILE = 1
,NORECOVERY
,NOUNLOAD
,REPLACE
,STATS = 5
GO

RESTORE DATABASE [fork_scenario]
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak'
WITH FILE = 2
,NOUNLOAD
,STATS = 5
GO

ALTER DATABASE [fork_scenario]

SET MULTI_USER
GO

USE fork_scenario
GO

INSERT INTO t
VALUES (3)

BACKUP LOG [fork_scenario] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak'
WITH NOFORMAT
,NOINIT
,NAME = N'fork_scenario-Transaction Log Backup'
,SKIP
,NOREWIND
,NOUNLOAD
,STATS = 10
GO

Reason: 

The file position numbers are not the same as the those for backup. They need to be the same.

Solution: 

In my case, the two file position numbers for full and differential backups are 12 and 13. The problem is fixed when I change the file position numbers as below. :

RESTORE DATABASE [fork_scenario] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak' WITH FILE = 12, NORECOVERY, NOUNLOAD, REPLACE, STATS = 5

GO

RESTORE DATABASE [fork_scenario] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\fork_scenario.bak' WITH FILE = 13, NOUNLOAD, STATS = 5

GO