A simple CROSS APPLY always works when run from master, but doesn't work when run from most (but not all) of my other user databases. I can't figure out what could be causing the error, or is there some reason it would only work when the current database is master?
Here is the simplest testcase I can come up with:
select a.spid,b.text from master..sysprocesses a cross apply ::fn_get_sql(a.sql_handle) b
If this is run while the current database is master, it returns spid and SQL without a problem. If it's run in the other SQL-delivered databases (model, msdb, tempb), it also works fine. However, if it's run in all but one of the user databases, I get back an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'a'.
I'm stumped on how to troubleshoot this. There is no difference in the query; the only difference is the current database. The databases are all owned by sa. I get the same results with sys.sysprocesses instead of master..sysprocesses.
As another datapoint, the employee/department example in the BOL for CROSS APPLY exhibits the same behavior; it works fine if run with master as the current database (regardless of where the tables are created), it fails with
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.
if run from one of the other user databases.
Any ideas on what could be wrong?
Thanks,
Vince
Sorry to reply in my own thread, but I found the problem. I took drastic measures and created a new, empty database, then deleted all objects out of one of the ones that didn't work. The new one worked, the old one didn't.
Then I started looking at every dialog in SMSS for each database. The difference (which everyone else has probably figured out already) was the compatability level. The old databases were created with 80, the new ones had 90.
My apologies for the noise.
|||Actually, it wasn't necessary to drop these databases - there is a special stored proc to change their compatibility level:
exec sp_dbcmptlevel MyOldDB, 90
No comments:
Post a Comment