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 Wednesday, July 6, 2011 12:58 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:08 AM
Points: 754, Visits: 3,816
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
Post #1137589
Posted Thursday, January 12, 2012 8:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Yesterday @ 9:16 AM
Points: 4, Visits: 77
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
Post #1234863
Posted Thursday, January 12, 2012 8:25 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:08 AM
Points: 754, Visits: 3,816
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
Post #1234875
Posted Thursday, January 12, 2012 9:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Yesterday @ 9:16 AM
Points: 4, Visits: 77
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
Post #1234937
Posted Thursday, January 12, 2012 9:18 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:47 AM
Points: 2,443, Visits: 7,556
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.



Not a DBA, just trying to learn

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/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1234971
Posted Thursday, January 12, 2012 9:53 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:08 AM
Points: 754, Visits: 3,816
jsteinberg (1/12/2012)
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


I wish that I had time right now to find those procedures and try to duplicate the error, but right now I just don't. Of course, whatever works for you, works for you. If it doesn't cause an error, then don't worry about it too much. Apparently this only happens in certain circumstances and I'd like to make duplicating the error a project for myself since I may have stumbled upon a SQL bug.

But think of it this way, you can name columns commands, for example, you can name a column YEAR, and they don't cause errors that I'm aware of. But why risk it? I normally avoid naming columns names that are also command or function names, just to be safe.


______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1235041
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse