Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying schema for temporary tables

I'm used to seeing temporary tables created with just the hash/number symbol, like this:

CREATE TABLE #Test
(
    [Id] INT
)

However, I've recently come across stored procedure code that specifies the schema name when creating temporary tables, for example:

CREATE TABLE [dbo].[#Test]
(
    [Id] INT
)

Is there any reason why you would want to do this? If you're only specifying the user's default schema, does it make any difference? Does this refer to the [dbo] schema in the local database or the tempdb database?

like image 285
Tom Hunter Avatar asked Nov 29 '11 13:11

Tom Hunter


1 Answers

It won't make any difference if you are specifying the users default schema, but if the users default schema changes then it will try to keep the temporary table in the dbo schema.

Temp tables are created in tempdb so it means you'd need to maintain the schema in tempdb, and offers no benefit.

like image 60
Stephen Turner Avatar answered Oct 20 '22 18:10

Stephen Turner