|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 03, 2013 9:46 AM
Points: 1,164,
Visits: 2,603
|
|
mbova407 (11/22/2010)
Correct sql is select * from tempdb..sysobjects where name like '%#testvm%'
Not quite.
It was mentioned in an earlier post that using LIKE and wildcards may return more than one record if another temporary table is created with a similar name. And in another post using the built-in function OBJECT_ID was proposed. Then Hugo brought up the fact that sysobjects is deprecated and sys.objects shouild be used instead.
So putting all this together we have:
select * from tempdb.sys.objects where object_id = OBJECT_ID('tempdb.dbo.#testvm') Hope I've not missed anything
-- Nigel Useful Links: How to post data/code on a forum to get the best help The "Numbers" or "Tally" Table - Jeff Moden
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 1,992,
Visits: 1,855
|
|
WayneS (11/22/2010)
BTW, the best way to check for the existance of a temporary table in your current session is: IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULLNote that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use: IF OBJECT_ID('tempdb..#YourTempTable') <> 0
You can test <> 0 I always use IF OBJECT_ID('tempdb..#YourTempTable') <> 0
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 1,992,
Visits: 1,855
|
|
Carlo Romagnano (11/22/2010)
WayneS (11/22/2010)
BTW, the best way to check for the existance of a temporary table in your current session is: IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULLNote that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use: IF OBJECT_ID('tempdb..#YourTempTable') > 0
You can test <> 0 I always use IF OBJECT_ID('tempdb..#YourTempTable') <> 0 
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 10:25 AM
Points: 485,
Visits: 1,569
|
|
So putting all this together we have: select * from tempdb.sys.objects where object_id = OBJECT_ID('tempdb.dbo.#testvm')Hope I've not missed anything 
seems perfect to me....
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 12:28 PM
Points: 1,058,
Visits: 1,394
|
|
Carlo Romagnano (11/22/2010)
Carlo Romagnano (11/22/2010)
WayneS (11/22/2010)
BTW, the best way to check for the existance of a temporary table in your current session is: IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULLNote that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use: IF OBJECT_ID('tempdb..#YourTempTable') > 0
You can test <> 0 I always use IF OBJECT_ID('tempdb..#YourTempTable') <> 0 
But as Wayne said, the best way is to use IS NOT NULL. Why? Because then you're testing for the defined behavior. OBJECT_ID() is designed to return NULL (NOT 0) for objects which do not exist or are not schema-scoped.
So if you test against 0, you're designing potential failure (there's no technical reason why Microsoft couldn't have SQL Server create an object with an ID of 0 -- it's just another number, after all) into your application for no good reason.
http://msdn.microsoft.com/en-us/library/ms190328.aspx
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 8:30 AM
Points: 228,
Visits: 179
|
|
Okay but where does the numbering at the end of the underscores correspond to the spid that created it?
Hypothetically, lets say I wanted to see what spid owned each copy of a local temp table. How would I do that? Also does it work for a global temp table.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 6,386,
Visits: 8,286
|
|
mbova407 (11/22/2010)
Correct sql is select * from tempdb..sysobjects where name like '%#testvm%'
Try this: in one query window, run:
CREATE TABLE #testvm (RowID INT IDENTITY);
In a different query window, run your command:
IF EXISTS (select * from tempdb..sysobjects where name like '%#testvm%') SELECT * FROM #testvm
As you will notice, your query does find a temp table - however you can't use it since it was created in another session.
Wayne Microsoft Certified Master: SQL Server 2008 If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it! Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines, CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 6,386,
Visits: 8,286
|
|
Seth Kramer (11/22/2010) Okay but where does the numbering at the end of the underscores correspond to the spid that created it?
Hypothetically, lets say I wanted to see what spid owned each copy of a local temp table. How would I do that? Also does it work for a global temp table.
Those last 12 characters do not correspond to the spid - they are sequentially assigned hexadecimal numbers based on the order that the temp tables are created. Run the following code (works best with results to text) to sequentially create the same temp table 100 times; you will see that the suffix appended to the table name is sequential.
SET NOCOUNT ON; GO if object_id('tempdb..#test') IS NOT NULL DROP TABLE #test; create table #test (RowID INT IDENTITY PRIMARY KEY CLUSTERED); select name from tempdb.sys.tables where name like '#test%'; GO 100
Wayne Microsoft Certified Master: SQL Server 2008 If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it! Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines, CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 7:11 AM
Points: 877,
Visits: 1,159
|
|
GO [count] is nice command to execute the batch no of times. Today's QoTD (11/23/2010) is also refer GO [count] statement.
Thanks
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:50 PM
Points: 1,063,
Visits: 4,256
|
|
| What interested me was how long the random names are in sysobjects - obviously Microsoft planning for LOTS of temporary objects being created!
|
|
|
|