I have noticed many interesting articles on temporary table in sql server.
But, this article provides some unknown facts on sql server.
Assuring you an interesting fact on sql server temporary table.
Know about Temporary table:
Temporary tables are special tables created in temp database. It has a prefix '#' for local temporary table
or "##" for global temporary table.
The temporary table will get deleted if the session is expired or the server restarts.
Local temporary table: This can be used with in a specific object and cannot be
shared with other system objects. Each table is associated with a specific session id
and we cannot use it in another session.
For ex: I am creating a temporary table A in the stored procedure SP1, If i try to
use the same temporary table in another stored procedure SP2. It won't work because
its not in this specific scope. To recover this problem, we have to Global temporary table.
Global temporary table: Its nothing but a global table accessed by different
system objects.
Ok, am stopping all those basics and lets enter into some R&D work,
I had a situation to construct a temporary table using dynamic SQL. OOPS!! am finding some
strange problem to achieve it. Let's see the code,
Code Snippet |
declare @string varchar(1000) set @string = 'SELECT * INTO #TempTable FROM dbo.SAMPLE' exec (@string) SELECT * from #TempTable |
I am getting the following error,
"Invalid object name '#TempTable'."
I wondered to see this message and started checking the syntax, but everything is
ok and i don't find any way to fix this problem.
I've tried with ordinary table,
Code Snippet |
declare @string varchar(1000) set @string = 'SELECT * INTO TempTable FROM dbo.SAMPLE' exec (@string) SELECT * from TempTable |
But my business functionality forces me to create a temporary table. I don't find
any other way to go,
Code Snippet |
declare @string varchar(1000) create table #TempTable(id int) set @string = 'insert INTO #TempTable select id FROM dbo.SAMPLE' exec (@string) SELECT * from #TempTable |
Digging towards it, i found some other way.
I resolved the problem by using global temporary table instead of local temporary table
It's working fine.
Code Snippet |
declare @string varchar(1000) set @string = 'SELECT * INTO ##TempTable FROM dbo.SAMPLE' exec (@string) SELECT * from ##TempTable |
After a thorough search, i found that the problem lies with the exec statement.
Exec statement will try to create a seperate session and my local temporary table
won't lie in this newly created session.
Interesting right!!!!!
Happy learning.
Regards,
Venkatesan Prabu . J
2 comments:
I assume, like me, you are using a temporary table so that more than one user can run the process without trying to create/access the same data. Problem is that a global temporary table is visible to everyone and, if someone instigates the process before a previous instigation is complete, they will get a "table already exists error".
Its no the case...
You will get seperate tables based on the session id maintained by the user
Thanks
Venkat
Post a Comment