SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Temp Table Name confusion with Nested Procedures?


Temp Table Name confusion with Nested Procedures?

Author
Message
mtillman-921105
mtillman-921105
SSC Eights!
SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)

Group: General Forum Members
Points: 828 Visits: 3852
Yes, writing code has its ways of promoting humility. :-D

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
jsteinberg
jsteinberg
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 78
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
mtillman-921105
mtillman-921105
SSC Eights!
SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)

Group: General Forum Members
Points: 828 Visits: 3852
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
jsteinberg
jsteinberg
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 78
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
Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3904 Visits: 8472
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

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


Craig Wilkinson - Software Engineer
LinkedIn
mtillman-921105
mtillman-921105
SSC Eights!
SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)SSC Eights! (828 reputation)

Group: General Forum Members
Points: 828 Visits: 3852
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search