Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associating Temp Table with a SPID

Status
Not open for further replies.

bikerboy718

Programmer
Feb 11, 2005
195
US
I have a server which has multiple databases. Each database has identical stored procedures that create temp tables in tempdb. Each temp table has a unique table name stored in the tempdb..sysobjects table for example:

#testtable______________________1234567
#testtable______________________2345678

How can I associate a temp table with the corresponding spid in which it was created?

With Great Power Comes Great Responsibility!!! [afro]

Michael
 
Why do you want to do this? Normally you would just leave these things alone.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Oh trust me I know. Long story short. We host a server that has a number of client databases. The application that connects to the database keeps a dedicated connection to the server open. The stored procedures that run on the server are tempdb intensive. If the users keep the sessions open (which is a very common problem) it never dumps the tempdb. I would like to put in drop table commands for the temp tables but I want to ensure that they exist first.

With Great Power Comes Great Responsibility!!! [afro]

Michael
 
I actually figured out how do to relate the session to the temp table instance in the tempdb. If you look up the table by object id from your session it will give you the id of the object from your session.

Code:
DECLARE @OBJID BIGINT
SELECT @OBJID = OBJECT_ID('TEMPDB..TABLENAME')
SELECT NAME FROM TEMPDB..SYSOBJECTS WHERE ID = @OBJID

With Great Power Comes Great Responsibility!!! [afro]

Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top