I have a local temporary table like this:
create table #tbl
(
employeeId int,
employeeName nvarchar(50)
)
insert into #tbl
values (1, 'a'), (2, 'b'),
(3, 'c'), (4, 'd')
select * from #tbl
Now I can access the local temporary table in that particular user session and in that particular query window compared to the global temporary table in which I can access the global temp table in any of the query window until the created query window gets closed, so far so good.
Now I am not able to understand the reason why one would require a global temporary table.
My questions are:
Is global temporary table instance specific? If yes then how can other user get hold of the newly created global temporary table
What is the reason SQL Server developed global temporary table over local temporary table?
Personally, I don't see a need for global temporary tables. My inclination is to store such data in tables along with other tables. But, clearly, they do fill a need.
The answer to your first question is that global temporary tables are instance specific -- if by instance you mean a SQL Server instance. They are available to all users and all connections on the server. Global temporary tables start with the prefix
##
. All references to such a table, say##table
, are to the same table (within a server instance).The answer to the second is that SQL Server supports both global and local temporary tables. Local temporary tables are more common. All temporary tables have the nice feature that they disappear when they are automatically deleted when the server restarts and under other circumstances.