﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by VM  / temp table in sysobjects / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 17:41:30 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Thanks for the question, and the discussion.</description><pubDate>Fri, 03 Dec 2010 13:20:40 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Well said Nigel. :-)</description><pubDate>Wed, 24 Nov 2010 21:13:19 GMT</pubDate><dc:creator>archana.singh 38798</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Thanks for the question.</description><pubDate>Wed, 24 Nov 2010 10:55:53 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]archana.singh 38798 (11/23/2010)[/b][hr]This will surely work : select * from sysobjects where name like '#testvm%'[/quote]No, because it will also return tables called '#testvm_something' etc. as was explained earlier</description><pubDate>Wed, 24 Nov 2010 01:51:13 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>This will surely work : select * from sysobjects where name like '#testvm%'</description><pubDate>Tue, 23 Nov 2010 23:52:45 GMT</pubDate><dc:creator>archana.singh 38798</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]jts_2003 (11/23/2010)[/b][hr]What interested me was how long the random names are in sysobjects - obviously Microsoft planning for LOTS of temporary objects being created![/quote]It's not the number of names, it's to lower the chances of generating duplicate names. That's why GUID/UNIQUEIDENTIFIER values are so long.</description><pubDate>Tue, 23 Nov 2010 19:02:13 GMT</pubDate><dc:creator>DLathrop</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>What interested me was how long the random names are in sysobjects - obviously Microsoft planning for LOTS of temporary objects being created!</description><pubDate>Tue, 23 Nov 2010 09:37:05 GMT</pubDate><dc:creator>jts_2003</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>GO [count] is nice command to execute the batch no of times. Today's QoTD (11/23/2010) is also refer GO [count] statement. :-)</description><pubDate>Tue, 23 Nov 2010 05:02:40 GMT</pubDate><dc:creator>Hardy21</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]Seth Kramer (11/22/2010)[/b][hr]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.[/quote]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.[code="sql"]SET NOCOUNT ON;GOif 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[/code]</description><pubDate>Mon, 22 Nov 2010 18:31:20 GMT</pubDate><dc:creator>WayneS</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]mbova407 (11/22/2010)[/b][hr]Correct sql is [code="sql"]select * from tempdb..sysobjects where name like '%#testvm%'[/code][/quote]Try this:in one query window, run:[code="sql"]CREATE TABLE #testvm (RowID INT IDENTITY);[/code]In a different query window, run your command:[code="sql"]IF EXISTS (select * from tempdb..sysobjects where name like '%#testvm%') SELECT * FROM #testvm[/code]As you will notice, your query does find a temp table - however you can't use it since it was created in another session.</description><pubDate>Mon, 22 Nov 2010 18:04:53 GMT</pubDate><dc:creator>WayneS</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>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.</description><pubDate>Mon, 22 Nov 2010 15:44:11 GMT</pubDate><dc:creator>Seth Kramer</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]Carlo Romagnano (11/22/2010)[/b][hr][quote][b]Carlo Romagnano (11/22/2010)[/b][hr][quote][b]WayneS (11/22/2010)[/b][hr]BTW, the best way to check for the existance of a temporary table in [u]your current session[/u] is:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULL[/code]Note that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;gt; 0[/code][/quote]You can test &amp;lt;&amp;gt; 0I always use [code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;lt;&amp;gt;  0[/code] :-D[/quote][/quote]But as Wayne said, the [b]best[/b] 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</description><pubDate>Mon, 22 Nov 2010 09:04:28 GMT</pubDate><dc:creator>sknox</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote] So putting all this together we have:[code]select * from tempdb.sys.objects where object_id = OBJECT_ID('tempdb.dbo.#testvm')[/code]Hope I've not missed anything :-)[/quote]seems perfect to me....</description><pubDate>Mon, 22 Nov 2010 08:20:10 GMT</pubDate><dc:creator>Geoff A</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]Carlo Romagnano (11/22/2010)[/b][hr][quote][b]WayneS (11/22/2010)[/b][hr]BTW, the best way to check for the existance of a temporary table in [u]your current session[/u] is:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULL[/code]Note that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;gt; 0[/code][/quote]You can test &amp;lt;&amp;gt; 0I always use [code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;lt;&amp;gt;  0[/code] :-D[/quote]</description><pubDate>Mon, 22 Nov 2010 07:54:45 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]WayneS (11/22/2010)[/b][hr]BTW, the best way to check for the existance of a temporary table in [u]your current session[/u] is:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULL[/code]Note that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;lt;&amp;gt; 0[/code][/quote]You can test &amp;lt;&amp;gt; 0I always use [code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;lt;&amp;gt; 0[/code] :-D</description><pubDate>Mon, 22 Nov 2010 07:53:26 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]mbova407 (11/22/2010)[/b][hr]Correct sql is [code="sql"]select * from tempdb..sysobjects where name like '%#testvm%'[/code][/quote]Not quite.It was mentioned in an earlier post that using [b]LIKE[/b] 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:[code]select * from tempdb.sys.objects where object_id = OBJECT_ID('tempdb.dbo.#testvm')[/code]Hope I've not missed anything :-)</description><pubDate>Mon, 22 Nov 2010 07:27:44 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Correct sql is [code="sql"]select * from tempdb..sysobjects where name like '%#testvm%'[/code]</description><pubDate>Mon, 22 Nov 2010 06:27:59 GMT</pubDate><dc:creator>mbova407</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>BTW, the best way to check for the existance of a temporary table in [u]your current session[/u] is:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') IS NOT NULL[/code]Note that in SQL Server thru version 2008R2, temp tables are created with a positive object_id value, so you might want to use:[code="sql"]IF OBJECT_ID('tempdb..#YourTempTable') &amp;gt; 0[/code]However, the next version of SQL (code named Denali) is returning an object_id with a negative value, so the &amp;gt;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.</description><pubDate>Mon, 22 Nov 2010 06:13:50 GMT</pubDate><dc:creator>WayneS</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Good question, thanks for taking the time to help educate the SQL community.</description><pubDate>Mon, 22 Nov 2010 05:27:30 GMT</pubDate><dc:creator>RichardDouglas</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]Randhir Singh (11/22/2010)[/b][hr]Good question!!!I just want to add few lines here..1. sysobjects is a view not table[/quote]And a deprecated one to boot. New code should use the new system views. In this case, sys.objects.</description><pubDate>Mon, 22 Nov 2010 04:55:37 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Good question!!!I just want to add few lines here..1. sysobjects is a view not table2. 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</description><pubDate>Mon, 22 Nov 2010 04:16:37 GMT</pubDate><dc:creator>Randhir Singh</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote]Below query returns the required result:[code="sql"]select * from tempdb..sysobjects where name like '#testvm%' [/code][/quote]Indeed Hardy21 it  works, I also tried with:[code="sql"]select * from tempdb..sysobjects where name like '#testvm' [/code]and observed it does not work, but then I saw the name column: #testvm__...__00000000000E :-)Thanks,Iulian</description><pubDate>Mon, 22 Nov 2010 03:37:39 GMT</pubDate><dc:creator>Iulian -207023</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]vk-kirov[/b][/quote]That's true!I ran it on sql2000 sp4.</description><pubDate>Mon, 22 Nov 2010 03:21:18 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]Carlo Romagnano (11/22/2010)[/b][hr]Any user can view sysobjects in tempdb:Open session ONE:create table #a(i int)select name,id from tempdb.dbo.sysobjects where name like '#a%'Open session TWO with same or another user:create table #a(i int)select name,id from tempdb.dbo.sysobjects where name like '#a%'[/quote]I ran these scripts on a test server (I'm not a sysadmin of that server) and got the following results (underscore characters are truncated).Session 1:[code="plain"]name                       id-------------------------- -----------#a____________0000000648F9 756038366[/code]Session 2:[code="plain"]name                       id-------------------------- -----------#a____________0000000648FA 772038423[/code]The version of the SQL Server:[code="sql"]SELECT @@VERSION[/code][quote]Microsoft SQL Server 2005 - 9.00.4053.00 (Intel IA-64)   May 26 2009 14:15:40   Copyright (c) 1988-2005 Microsoft Corporation  Developer Edition (64-bit) on Windows NT 5.2 (Build 3790: Service Pack 1)[/quote]</description><pubDate>Mon, 22 Nov 2010 02:58:17 GMT</pubDate><dc:creator>vk-kirov</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Good one point question.Below query returns the required result:[code="sql"]select * from tempdb..sysobjects where name like '#testvm%'[/code]</description><pubDate>Mon, 22 Nov 2010 02:29:39 GMT</pubDate><dc:creator>Hardy21</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>I got the right answer but for the wrong reason--I thought temporary items wouldn't show up in sysobjects, so I learned something today! :-)</description><pubDate>Mon, 22 Nov 2010 02:14:06 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]vk-kirov (11/22/2010)[/b][hr][quote][b]Carlo Romagnano (11/22/2010)[/b][hr]Also this is wrong:[quote]If you used a query like this, you would see [b]your table[/b]: [/quote]You will see [b]ALL tables[/b] that begins with '#testvm', also, tables belonging to other sessions.[/quote]... only if you are a privileged user on the server (a member of the [b]sysadmin[/b] role, I assume). If you are an ordinary user, you can see only the temporary tables which have been created in your current connection.[/quote]Any user can view sysobjects in tempdb:Open session ONE:create table #a(i int)select name,id from tempdb.dbo.sysobjects where name like '#a%'Open session TWO with same or another user:create table #a(i int)select name,id from tempdb.dbo.sysobjects where name like '#a%'Here the result:name	id#a__________________________________________________________________________________________________________________0000000A6831	86071260#a__________________________________________________________________________________________________________________0000000A6855	955646365</description><pubDate>Mon, 22 Nov 2010 02:05:35 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[quote][b]Carlo Romagnano (11/22/2010)[/b][hr]Also this is wrong:[quote]If you used a query like this, you would see [b]your table[/b]: [/quote]You will see [b]ALL tables[/b] that begins with '#testvm', also, tables belonging to other sessions.[/quote]... only if you are a privileged user on the server (a member of the [b]sysadmin[/b] role, I assume). If you are an ordinary user, you can see only the temporary tables which have been created in your current connection.</description><pubDate>Mon, 22 Nov 2010 01:42:51 GMT</pubDate><dc:creator>vk-kirov</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Right This query is not working if the query is running in another DB sessionand also this syntax is wrong select * from sysobjects where name = '#testvm%'if the query is like select * from sysobjects where name like '#testvm%'and the session is in tempdb then it will work otherwise it will be like bellowselect * from tempdb..sysobjects where name like '#testvm%'Yousaf Khan</description><pubDate>Mon, 22 Nov 2010 01:41:18 GMT</pubDate><dc:creator>Yousaf Khan</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Also this is wrong:[quote]If you used a query like this, you would see [b]your table[/b]: [/quote]You will see [b]ALL tables[/b] that begins with '#testvm', also, tables belonging to other sessions.</description><pubDate>Mon, 22 Nov 2010 01:06:04 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>[code="sql"]select * from sysobjects where name = '#testvm%'[/code]This query doesn't work and should be replaced with the following:[code="sql"]select * from tempdb.dbo.sysobjects where name like '#testvm%'[/code]</description><pubDate>Mon, 22 Nov 2010 00:48:35 GMT</pubDate><dc:creator>vk-kirov</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Nice Question .There is a another way to Check that , using the below query create table #testvm (ID int)select * from tempdb.sys.objects where OBJECT_id= object_id('tempDB..#testvm')drop table #testvmThanks &amp; RegardsDeepak.A</description><pubDate>Mon, 22 Nov 2010 00:17:33 GMT</pubDate><dc:creator>deepak.a</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>I agree with Scott, the query which the explanation says willprovide info about the table will in fact retrieve nothing unless "=" is changed to "LIKE" and it's running in the context of tempdb.</description><pubDate>Sun, 21 Nov 2010 13:19:06 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Good question. Couple of issues with the construction of it though:1. Unless the database context is set to tempdb, or you explicitly reference tempdb..sysobjects, you will get no rows returned.2. If you are using the wildcard (%) character, the '=' will need to be changed to 'LIKE'. Again, no rows returned otherwise.</description><pubDate>Sun, 21 Nov 2010 12:56:45 GMT</pubDate><dc:creator>The CosmicTrickster</dc:creator></item><item><title>RE: temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Nice question, although a link to a reference would have been nice.(ps: quite bizare I answered the QoTD of 22th November on the 20th of November. Time travelling? :-))</description><pubDate>Sat, 20 Nov 2010 12:31:28 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>temp table in sysobjects</title><link>http://www.sqlservercentral.com/Forums/Topic1023959-1495-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/71658/"&gt;temp table in sysobjects&lt;/A&gt;[/B]</description><pubDate>Sat, 20 Nov 2010 12:30:20 GMT</pubDate><dc:creator>VM-723206</dc:creator></item></channel></rss>