views:

425

answers:

3

I was looking at the source of sys.sp_dbcmptlevel in SQL Server 2005.

In the source, there is this line I do not understand how it works.

EXEC %%DatabaseEx(Name = @dbname).SetCompatibility(Level = @input_cmptlevel)

It doesn't appear that DatabaseEx is a stored procedure.

-- does not return any result
select *
from sys.procedures
where [name] like '%DatabaseEx%'

So my questions are

  • What is DatabaseEx and what does it do?
  • What is %% before DatabaseEx?
+1  A: 
-- Note: database @dbname may not exist anymore
-- Change compatibility level
-- If invoke gets error, exception will abort this proc.
EXEC %%DatabaseEx(Name = @dbname).SetCompatibility(Level = @input_cmptlevel)

it looks like a way to refer to a variable database as an object and make config changes

KM
+1  A: 

Interesting find.

System SP's also refer to %%Object, %%Relation, %%ColumnEx, %%LinkedServer, %%Owner, %%CurrentDatabase(), %%ErrorMessage, %%Module, %%DatabaseRef, %%LocalLogin, %%Alias, %%ServerConfiguration, %%IndexOrStats, %%ScalarType (etc)

My interpretation is that the %%() retrieves some kind of (COM?) object based on filter criteria, followed by a method call.

devio
I was thinking that it was accessing an extended sproc but I am even sure about that at this point.
Sung Meister
+6  A: 

I think the best answer here is that it's not documented, and not supported, so don't rely on it. While it's interesting to know how SQL Server works internally, anything you do with that knowledge has the potential to break in a future hotfix, service pack or release.

Mike Dimmick
You're right about that, Mike. I just wanted to know what it does but wasn't going to use it in a production script.
Sung Meister