Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "%%DatabaseEx" do in TSQL?

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?
like image 911
dance2die Avatar asked Feb 03 '23 11:02

dance2die


2 Answers

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.

like image 110
Mike Dimmick Avatar answered Feb 06 '23 00:02

Mike Dimmick


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.

like image 23
devio Avatar answered Feb 06 '23 02:02

devio