Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Invalid SQL Logic - Temp Tables

$
0
0

Could someone explain WHY SQL does not obey logic:

Code that Fails:

IF OBJECT_ID('tempdb..##SCDITL') IS NOT NULL
	DROP TABLE ##SCDITL
IF OBJECT_ID('tempdb..##SCDITLTemp') IS NOT NULL
	DROP TABLE ##SCDITLTemp

SELECT C.ObjectId, L.Name, ROW_NUMBER() OVER (PARTITION BY C.ObjectId ORDER BY L.Name) RNK
INTO ##SCDITLTemp
FROM SlotComponentDef C
		INNER JOIN ITLReport R ON R.SlotComponentDef = C.ObjectId
		INNER JOIN ITL L ON R.ITL = L.ObjectId
		
DECLARE @MaxRNK int
SELECT @MaxRNK = MAX(RNK) FROM ##SCDITLTemp		

IF @MaxRNK > 1 
BEGIN
	CREATE TABLE ##SCDITL (ObjectId UNIQUEIDENTIFIER, LabName NVARCHAR(1024))
	WHILE (@MaxRNK > 0) 
	BEGIN
		-- Other code here not important
		SET @MaxRNK = @MaxRNK - 1
	END
END
ELSE SELECT ObjectId, Name [LabName] INTO ##SCDITL FROM ##SCDITLTemp --this is the line of explosion

SELECT * FROM ##SCDITLTemp
SELECT * FROM ##SCDITL

Error:

Msg 2714, Level 16, State 1, Line 35
There is already an object named '##SCDITL' in the database.

Code that Succeeds:

IF OBJECT_ID('tempdb..##SCDITL') IS NOT NULL
	DROP TABLE ##SCDITL
IF OBJECT_ID('tempdb..##SCDITLTemp') IS NOT NULL
	DROP TABLE ##SCDITLTemp

SELECT C.ObjectId, L.Name, ROW_NUMBER() OVER (PARTITION BY C.ObjectId ORDER BY L.Name) RNK
INTO ##SCDITLTemp
FROM SlotComponentDef C
		INNER JOIN ITLReport R ON R.SlotComponentDef = C.ObjectId
		INNER JOIN ITL L ON R.ITL = L.ObjectId
		
DECLARE @MaxRNK int
SELECT @MaxRNK = MAX(RNK) FROM ##SCDITLTemp		

IF @MaxRNK > 1 
BEGIN
	CREATE TABLE SCDITL (ObjectId UNIQUEIDENTIFIER, LabName NVARCHAR(1024))
	WHILE (@MaxRNK > 0) 
	BEGIN
		-- Other code here not important
		SET @MaxRNK = @MaxRNK - 1
	END
END
ELSE SELECT ObjectId, Name [LabName] INTO SCDITL FROM ##SCDITLTemp

SELECT * FROM ##SCDITLTemp
SELECT * FROM SCDITL

Difference? The Temp Tables ##SCDITL in the first example I changed to being a REGULAR table without the (##).  So why does the global temp table moniker of ## change the behavior of the IF...ELSE clause surrounding the CREATE/SELECT INTO.  What kind of BS backwards logic is operating here that fundamentally ALTERS the behavior of the IF statement executing the CREATE TABLE prior to the IF condition being evaluated simply because the table being created is a temp table?  And yes, I checked, the first example runs without complaint if I comment out the CREATE TABLE line, but by all logic that line SHOULD NOT interfere with the SELECT INTO statement since they are two separate paths and both cannot be executed.

Why was this SNAFU ever allowed past the dev-team door? 

Jaeden "Sifo Dyas" al'Raec Ruiner


"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>