@@tempdbid global variable

The @@tempdbid returns a valid temporary database ID (dbid) of the session’s assigned temporary database.

Examples

Example 1 Returns the dbid of mytempdb, the session’s assigned temporary database, which is 7:

select @@tempdbid from mytempdb
7

Example 2 Returns the name of the temporary database, which is mytempdb:

select db_name(@@tempdbid)
mytempdb