Child sProc cannot reference a Local temp table created in parent sProc

Posted by John Galt on Stack Overflow See other posts from Stack Overflow or by John Galt
Published on 2010-05-21T17:49:39Z Indexed on 2010/05/21 17:50 UTC
Read the original article Hit count: 312

On our production SQL2000 instance, we have a database with hundreds of stored procedures, many of which use a technique of creating a #TEMP table "early" on in the code and then various inner stored procedures get EXECUTEd by this parent sProc. In SQL2000, the inner or "child" sProc have no problem INSERTing into #TEMP or SELECTing data from #TEMP. In short, I assume they can all refer to this #TEMP because they use the same connection.

In testing with SQL2008, I find 2 manifestations of different behavior. First, at design time, the new "intellisense" feature is complaining in Management Studio EDIT of the child sProc that #TEMP is an "invalid object name". But worse is that at execution time, the invoked parent sProc fails inside the nested child sProc.

Someone suggested that the solution is to change to ##TEMP which is apparently a global temporary table which can be referenced from different connections.

That seems too drastic a proposal both from the amount of work to chase down all the problem spots as well as possible/probable nasty effects when these sProcs are invoked from web applications (i.e. multiuser issues).

Is this indeed a change in behavior in SQL2005 or SQL2008 regarding #TEMP (local temp tables)? We skipped 2005 but I'd like to learn more precisely why this is occuring before I go off and try to hack out the needed fixes. Thanks.

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about sql-server-2005