Temp Table Name confusion with Nested Procedures?

  • 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

  • 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[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • 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

  • I'm just glad I decided to test it before I answered... or I'd have been wrong. :hehe:

    Seth Phelabaum


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

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Garadin (7/1/2011)


    I'm just glad I decided to test it before I answered... or I'd have been wrong. :hehe:

    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. :w00t:

    The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking

  • OK, now I wish that I could borrow the head-desk avatar from WayneS! :angry:

    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

  • 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. :exclamationmark:

    The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking

  • mtillman-921105 (7/1/2011)


    OK, now I wish that I could borrow the head-desk avatar from WayneS! :angry:

    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
    Author - SQL Server T-SQL Recipes


    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
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • WayneS (7/5/2011)


    mtillman-921105 (7/1/2011)


    OK, now I wish that I could borrow the head-desk avatar from WayneS! :angry:

    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

  • 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

  • Yes, writing code has its ways of promoting humility. 😀

    Since I've taught myself most of what I know about SQL Server, I'm bound to have a crack to fill here and there. And at times I have to say to myself "Hmm, I should have known that. A long time ago too!" This is one of those times.

    The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking

  • Can you possibly explain what that means?

    On the one hand, BOL says

    "All references to the table name in the nested stored procedure are resolved to the table created in the nested procedure"

    So the temp table in the nested proc is always the one created in that proc.

    Under what circustances does SQL server get confused?

    Thanks

  • This is an older thread, but I'll try to give an example and explain more.

    If you have one temp table named #Summary in the "parent" or calling procedure, then it is not a good idea to have a temp table with the same name in the "child" procedure (or the procedure which is being called).

    Maybe books online is referring to regular, non-temp, tables. But experience shows that having the same table names is potentially an issue. At the very least, I would avoid that just to be safe.

    The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking

  • Thanks for the response, but I'm afraid to say that this doesn't answer my question.

    According to the BOL documentation I quoted., temp tables in nested procs should never conflict based on name. I'm uncomfortable with your assertion that there is something wrong with a practice that is documented to be safe.

    A small code example of where this could cause a problem would be apprecated

    Thanks

  • mtillman-921105 (1/12/2012)


    This is an older thread, but I'll try to give an example and explain more.

    If you have one temp table named #Summary in the "parent" or calling procedure, then it is not a good idea to have a temp table with the same name in the "child" procedure (or the procedure which is being called).

    Maybe books online is referring to regular, non-temp, tables. But experience shows that having the same table names is potentially an issue. At the very least, I would avoid that just to be safe.

    I can't reproduce what you're talking about, and BOL doesn't appear to agree with your statement. Do you have a reproducible script?

    Here is what I tried: -

    BEGIN TRAN

    GO

    CREATE PROCEDURE Child

    AS

    BEGIN

    CREATE TABLE #test (ID INT)

    INSERT INTO #test

    SELECT 2

    SELECT *, 'Child'

    FROM #test

    END

    GO

    CREATE PROCEDURE Parent

    AS

    BEGIN

    CREATE TABLE #test (somethingElse VARCHAR(100))

    INSERT INTO #test

    SELECT 'One'

    EXECUTE Child

    INSERT INTO #test

    SELECT 'Two'

    SELECT *, 'Parent'

    FROM #test

    END

    GO

    DECLARE @i INT

    SET @i = 0

    CREATE TABLE #test (somethingDifferent INT)

    WHILE @i < 3

    BEGIN

    INSERT INTO #test

    SELECT @i

    SELECT *, 'NonSproc' FROM #test

    EXECUTE Parent

    SELECT *, 'NonSproc' FROM #test

    SET @i = @i + 1

    END

    ROLLBACK

    This executes with no problem, producing the results expected.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply