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»»

Temp Table Name confusion with Nested Procedures? Expand / Collapse
Author
Message
Posted Friday, July 1, 2011 9:33 AM


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: Monday, July 14, 2014 10:08 AM
Points: 598, Visits: 3,816
Normally I ask this type of question on Ask SSC, but I thought I'd try one here for a change.

I have a procedure that was working fine yesterday, now it breaks. It looks like the engine's getting confused about a temp table name today. If I have a local temp table with the same name, say #MyTemp, in both the calling and called nested procedure, will the engine choke on that? It seems that I've seen this before and had to change one of the names to resolve the issue.

I know that it would be best practice to use different names just in case, but it would seem that SQL could keep that straight rather than getting confused. Also, it was working just fine yesterday.

Thanks for your time!


______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1135136
Posted Friday, July 1, 2011 9:47 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, September 26, 2014 12:49 PM
Points: 1,519, Visits: 4,074
Temp tables created in the outer SP are available to the Inner SP, so you should definitely avoid this, as creating one with the same name will be confusing... but it doesn't seem to actually error. What's the error you're getting? See below example.

CREATE PROC #B
AS

SELECT * FROM #MyTemp
SELECT 'B' B INTO #MyTemp
SELECT * FROM #MyTemp

SELECT * FROM tempdb.sys.objects WHERE name LIKE '%MyTemp%'
GO

CREATE PROC #A
AS

SELECT 'A' A INTO #MyTemp

EXEC #B
SELECT * FROM #MyTemp
GO

EXEC #A

DROP PROC #B
DROP PROC #A



Seth Phelabaum
Consistency is only a virtue if you're not a screwup.

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Post #1135153
Posted Friday, July 1, 2011 9:53 AM


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: Monday, July 14, 2014 10:08 AM
Points: 598, Visits: 3,816
Thank you Garadin, I actually changed the name of the calling procedure's temp table and am still getting the error. So I must have been wrong about that - let me investigate further before I stick my foot in my mouth (again!) here.

______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1135159
Posted Friday, July 1, 2011 10:00 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, September 26, 2014 12:49 PM
Points: 1,519, Visits: 4,074
I'm just glad I decided to test it before I answered... or I'd have been wrong.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup.

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Post #1135164
Posted Friday, July 1, 2011 10:12 AM


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: Monday, July 14, 2014 10:08 AM
Points: 598, Visits: 3,816
Garadin (7/1/2011)
I'm just glad I decided to test it before I answered... or I'd have been wrong.


Ha! Don't feel like the Lone Stranger on that one - I have to do that too all too often myself.

Anyway, the error is "Invalid column name 'PGID'." on the called procedure

So if I open the called (or child?) procedure (from the server to be sure it's the compiled program) and run it, no errors. If I call it manually from an exec, it chokes. So it may just be a parameter value snafu.

Parameters are fantastic! And yet, they're such a pain sometimes.


______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1135174
Posted Friday, July 1, 2011 1:15 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: Monday, July 14, 2014 10:08 AM
Points: 598, Visits: 3,816
OK, now I wish that I could borrow the head-desk avatar from WayneS!

After getting fed up with verifying the parameters and parameter values over and over. I closed the procedure without saving it, opened it back up and it works perfectly. (!) 'Was running perfectly yesterday and I ran it without change this morning, but it balked. Anyway, the answer is a mystery.



______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1135333
Posted Tuesday, July 5, 2011 10:31 AM


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: Monday, July 14, 2014 10:08 AM
Points: 598, Visits: 3,816
OK, this procedure and it's nested cousin caused issues again this morning. Here is the odd part, the proc. running through Crystal Reports ran just fine. I open the procedure in SSMS, again straight from the server to be sure it is indeed the code in question) and it errors out.

I changed the name of the temp table that I thouht it was confused about (so that the two procs were not using a temp table with the same name) and it runs fine. So it looks like the SQL Engine can indeed get confused that way.


______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1136658
Posted Tuesday, July 5, 2011 12:00 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:18 AM
Points: 5,361, Visits: 8,926
mtillman-921105 (7/1/2011)
OK, now I wish that I could borrow the head-desk avatar from WayneS!

Feel free to borrow it anytime... it's at http://www.dreamwidth.org/userpic/21008/20791
(Sometimes, I'd like to change my avatar to this one - but I think Steve would ask me to change it.)
[evil grin]Hmm... isn't Steve on vacation right now?[/evil grin]


Anyway, to answer your question: From BOL:CREATE TABLE
A local temporary table created within a stored procedure or trigger can have the same name as a temporary table that was created before the stored procedure or trigger is called. However, if a query references a temporary table and two temporary tables with the same name exist at that time, it is not defined which table the query is resolved against.

(emphasis mine)


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
Post #1136733
Posted Tuesday, July 5, 2011 12:07 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: Monday, July 14, 2014 10:08 AM
Points: 598, Visits: 3,816
WayneS (7/5/2011)
mtillman-921105 (7/1/2011)
OK, now I wish that I could borrow the head-desk avatar from WayneS!

Feel free to borrow it anytime... it's at http://www.dreamwidth.org/userpic/21008/20791
(Sometimes, I'd like to change my avatar to this one - but I think Steve would ask me to change it.)
[evil grin]Hmm... isn't Steve on vacation right now?[/evil grin]


Anyway, to answer your question: From BOL:CREATE TABLE
A local temporary table created within a stored procedure or trigger can have the same name as a temporary table that was created before the stored procedure or trigger is called. However, if a query references a temporary table and two temporary tables with the same name exist at that time, it is not defined which table the query is resolved against.

(emphasis mine)


Thank you Wayne! That explains it. It's a mystery to my why this hasn't happened to me more frequently. But I've learned my lesson and at least there's an explanation.

Hilarious .gif by the way. I'll have to share that one.




______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1136740
Posted Wednesday, July 6, 2011 12:01 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, September 28, 2014 1:41 PM
Points: 959, Visits: 2,888
I got burned on this the hard way. A temp table was created in a trigger. I used a very simple table name like #T for temp.

I had such a table created in SSMS with the same name I was using for analysis purposes with, of course, a different set of columns.

When I ran an update on the table with the trigger I kept getting the error with invalid column name.

I was very embarrassed when I figured out that the trigger was inheriting the #T table on the same connection. Ooops!
Todd Fifield
Post #1137542
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse