Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

TSQL - tempdb Expand / Collapse
Author
Message
Posted Friday, February 27, 2009 8:28 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 12:37 PM
Points: 2,391, Visits: 17,929
Gaby Abed (2/27/2009)
How many rows would this return? :) (yes...it's different)

use AdventureWorks
go
declare @tableA TABLE(col1 int, col2 int)
go
--stmt 1
select name from tempdb.sys.tables where name like '%tableA%'
--stmt 2
select name from tempdb.sys.columns where name like 'col1%'



I must admit - you TOTALLY had me... I was sitting here going "what??!?!", nothing is different! I even pasted the selects next to each other to make sure they were the same and hadn't changed. So why the different results?

I guess you can tell me "where to go". :D
GOT ME!

Chad
Post #665783
Posted Friday, February 27, 2009 8:34 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, September 8, 2014 11:08 AM
Points: 644, Visits: 2,134
Chad Crawford (2/27/2009)
Gaby Abed (2/27/2009)
How many rows would this return? :) (yes...it's different)

use AdventureWorks
go
declare @tableA TABLE(col1 int, col2 int)
go
--stmt 1
select name from tempdb.sys.tables where name like '%tableA%'
--stmt 2
select name from tempdb.sys.columns where name like 'col1%'



I must admit - you TOTALLY had me... I was sitting here going "what??!?!", nothing is different! I even pasted the selects next to each other to make sure they were the same and hadn't changed. So why the different results?

I guess you can tell me "where to go". :D
GOT ME!

Chad


Yeah, that second go screws you up with table variables. I usually use table variables for very short routines, but they are limited. First in persistence where that second go gets rid of it. The other is that a query similar to this will fail:

insert into @someTableVariable
exec someStoredProcecdure

This to me more than anything is the biggest limitation of a table variable. You can't capture values from a stored proc into it.


Gaby
________________________________________________________________
"In theory, theory and practice are the same. In practice, they are not."
- Albert Einstein
Post #665787
Posted Friday, February 27, 2009 12:10 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 6:27 AM
Points: 568, Visits: 813
If I ran these three SQL statements in one transaction, First returns nothing, second returns "col1".

Otherwise, both return nothing.
Post #665934
Posted Friday, February 27, 2009 12:13 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 10:47 AM
Points: 382, Visits: 1,159
WayneS (2/27/2009)
Interesting.

I modified the supplied code to get the # of tempdb objects, and all tempdb table names:
use AdventureWorks
go
select count(*) from tempdb.sys.objects
select name from tempdb.sys.tables --where name like '%tableA%'
declare @tableA TABLE(col1 int, col2 int)
--stmt 1
select name from tempdb.sys.tables --where name like '%tableA%'
--stmt 2
select name from tempdb.sys.columns where name like 'col1%'
select count(*) from tempdb.sys.objects

and I find that there are no new tables or objects between the two selects from sys.tables.

So I modified the sys.columns select to:
select sc.name [ColName], st.name [TableName] 
from tempdb.sys.columns sc
INNER JOIN tempdb.sys.tables st ON st.object_id = sc.object_id
where sc.name like 'col1%'

What I'm seeing is that the tablename for this table is already in use before the table is declared. I've even tried stopping and restarting the sql service for this instance.

What's up with this?


I think the (lack of ) batch separator is causing this.
Try this:

select sc.name [ColName], st.name [TableName]
from tempdb.sys.columns sc
INNER JOIN tempdb.sys.tables st ON st.object_id = sc.object_id
where sc.name like 'col1%'
declare @tableA TABLE(col1 int, col2 int)
select sc.name [ColName], st.name [TableName]
from tempdb.sys.columns sc
INNER JOIN tempdb.sys.tables st ON st.object_id = sc.object_id
where sc.name like 'col1%'

In the above, although we expect to see data for only the second select, we get data from both selects.

In the below select, if you separate the select between batches then you can see the count change.

select count(*) from tempdb.sys.objects
go
declare @tableA TABLE(col1 int, col2 int)
select count(*) from tempdb.sys.objects
go




Sankar Reddy | http://SankarReddy.com/
Post #665938
Posted Friday, March 6, 2009 1:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:41 PM
Points: 1,219, Visits: 1,430
If I run these statements I get zero and zero, etiher separately or as a batch.
If I add in the GO immediatly after the DECLARE, I still get zero and zero - because the table variable is no longer in scope.
If I add in place of the GO, SELECT * FROM @TableA then I get zero and 1 - the col1 in sys.columns only appears when I use the table.

Still I now know what the question was attempting to test: I found my temp table @tableA eventually and its called #32DA39A

Using SQL Server 2005, compat level 90
Post #669997
Posted Thursday, November 4, 2010 9:01 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Sunday, November 17, 2013 11:53 AM
Points: 623, Visits: 237
Good question
Post #1016371
Posted Tuesday, June 21, 2011 6:49 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 12:18 AM
Points: 375, Visits: 116
For 2005, i get 0 rows returned for both the statements. Why?
Post #1128957
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse