November 22, 2010 at 4:16 am
Good question!!!
I just want to add few lines here..
1. sysobjects is a view not table
2. We can list all database objects using this view but for current database, if we want to see database objects then we need to add reference.
Like Select * From tempdb..sysobjects
3. Adding a filter xType ='U', list all temp tables to tempdb database.
4. We can also use "Type" column instead of "xType", both are same but Type is for backward compatibility.
------------
Randhir Singh
November 22, 2010 at 4:55 am
Randhir Singh (11/22/2010)
Good question!!!I just want to add few lines here..
1. sysobjects is a view not table
And a deprecated one to boot. New code should use the new system views. In this case, sys.objects.
November 22, 2010 at 5:27 am
November 22, 2010 at 6:13 am
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 NULL
Note 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
However, the next version of SQL (code named Denali) is returning an object_id with a negative value, so the >0 won't work there, and it will need to be recoded.
Every other method I've seen for checking for the existence of a temp table fails when the temp table is created in a different sessions - those methods either crash, or return a positive (yes, it exists) incorrectly.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 22, 2010 at 6:28 am
Correct sql is
select * from tempdb..sysobjects where name like '%#testvm%'
November 22, 2010 at 7:27 am
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 🙂
November 22, 2010 at 7:53 am
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 NULL
Note 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
😀
November 22, 2010 at 7:54 am
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 NULL
Note 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
😀
November 22, 2010 at 8:20 am
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....
November 22, 2010 at 9:04 am
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 NULL
Note 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.
November 22, 2010 at 3:44 pm
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.
November 22, 2010 at 6:04 pm
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
Author - SQL Server T-SQL Recipes
November 22, 2010 at 6:31 pm
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
Author - SQL Server T-SQL Recipes
November 23, 2010 at 5:02 am
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
November 23, 2010 at 9:37 am
What interested me was how long the random names are in sysobjects - obviously Microsoft planning for LOTS of temporary objects being created!
Viewing 15 posts - 16 through 30 (of 35 total)
You must be logged in to reply to this topic. Login to reply