Hello,
The following query returns no result and no error on SQL Server 2008 (tested on SP1), you can run it against any database, even master:
WITH computed_table (id) AS
(
SELECT id FROM this_table_does_not_exist
)
(SELECT * FROM computed_table)
UNION
(SELECT * FROM another_table_that_does_not_exists)
On SQL Server 2005, you get an error because the tables do not exist. You get also get an error if you remove some parentheses:
WITH computed_table (id) AS
(
SELECT id FROM this_table_does_not_exist
)
SELECT * FROM computed_table
UNION
(SELECT * FROM another_table_that_does_not_exists)
The same kind of problems appears with real tables: on some occasions, the query does not return any result, and if you make some some slight changes, like removing a space or a carriage return, it works again...
I think that there may be a problem in the query, because the SELECT enclosed in parentheses may be interpreted as an expression instead of a subquery, as in this page. But that should at least return an error.
Am I missing something?
Edit 26/06/2010: I ran some profiling sessions, with the following results.
For the query above, the sequence of events is:
- Exception (Error: 208, invalid object name)
- SQL:BatchStarting
- SQL:StmtStarting
- SQL:BatchCompleted
For the query without parentheses:
- Exception (Error: 208)
- SQL:BatchStarting
- SQL:StmtStarting
- Exception (Error: 208)
- User Error Message (Invalid object name 'this_table_does_not _exist')
- SQL:BatchCompleted
For a working query:
- SQL:BatchStarting
- SQL:StmtStarting
- Showplan All
- SQL:StmtCompleted
- SQL:BatchCompleted
I also ran one of the queries with real tables that's causing me the same issue. The sequence of events is:
- SQL:BatchStarting
- SQL:StmtStarting
- SQL:BatchCompleted
No early "Exception" => the tables exist. No "SQL:StmtCompleted" => it means that an error occured, I could not see any other reason why this event would not be raised. No "Showplan All" => it means that the error occurs before (or when) the execution plan is computed. It may be caused by the combination of cte and parentheses.
I will raise the issue with Microsoft support next week.