November 1, 2010 at 7:19 pm
Craig Farrell (11/1/2010)
Of course to make my life interesting, this gets grumpy too and I can't remember how to get it to behave offhand. Maybe someone else remembers how to get this to behave without GO statements to split the script.
CREATE TABLE #tmp (tID INT);
DROP TABLE #tmp;
CREATE TABLE #tmp (tID INT, tText VARCHAR(200));
I don't know about 2k8 but the code above causes the following error in 2K5 sp3...
[font="Courier New"]Msg 2714, Level 16, State 1, Line 3
There is already an object named '#tmp' in the database.[/font]
--Jeff Moden
Change is inevitable... Change for the better is not.
November 2, 2010 at 10:41 am
Craig,
Thanks for the clarification.
Thanks & Regards,
MC
November 2, 2010 at 11:15 am
Jeff Moden (11/1/2010)
Craig Farrell (11/1/2010)
Of course to make my life interesting, this gets grumpy too and I can't remember how to get it to behave offhand. Maybe someone else remembers how to get this to behave without GO statements to split the script.
CREATE TABLE #tmp (tID INT);
DROP TABLE #tmp;
CREATE TABLE #tmp (tID INT, tText VARCHAR(200));
I don't know about 2k8 but the code above causes the following error in 2K5 sp3...
[font="Courier New"]Msg 2714, Level 16, State 1, Line 3
There is already an object named '#tmp' in the database.[/font]
Still fails in 2k8 SP1. I don't have R2 to test there.
November 2, 2010 at 11:20 am
Craig Farrell (11/1/2010)
Of course to make my life interesting, this gets grumpy too and I can't remember how to get it to behave offhand. Maybe someone else remembers how to get this to behave without GO statements to split the script.
CREATE TABLE #tmp (tID INT);
DROP TABLE #tmp;
CREATE TABLE #tmp (tID INT, tText VARCHAR(200));
The only option I see is to drop / alter the columns with the new definition or use a 2nd temp table, assuming the insert hit is low.
November 2, 2010 at 11:35 am
Yeah, even this doesn't work:
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp
END;
IF Object_id('#tmp') IS NULL
BEGIN
CREATE TABLE #tmp(tID INT, tTxt VARCHAR(50))
END;
So, I'm going to go with I had a bit of crack smoking going on for the double declare. Apparently the pre-compiler has an issue with a dual declare on the same table at any point. Probably a good thing. Not sure why I remembered doing that unless it was cascading procs.
So, sorry for that. The rest was accurate.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
November 2, 2010 at 11:43 am
Craig Farrell (11/2/2010)
Yeah, even this doesn't work:
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp
END;
IF Object_id('#tmp') IS NULL
BEGIN
CREATE TABLE #tmp(tID INT, tTxt VARCHAR(50))
END;
So, I'm going to go with I had a bit of crack smoking going on for the double declare. Apparently the pre-compiler has an issue with a dual declare on the same table at any point. Probably a good thing. Not sure why I remembered doing that unless it was cascading procs.
So, sorry for that. The rest was accurate.
Well you can't say I didn't try!
CREATE TABLE #tmp (tID INT);
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp
END;
IF Object_id('tempdb..#tmp_tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp_tmp
END;
CREATE TABLE #tmp_tmp(tID INT, tTxt VARCHAR(50))
exec sp_rename 'tempdb..#tmp', 'tempdb..#tmp', 'OBJECT'
SELECT * FROM #tmp
And no that doesn't work either
November 2, 2010 at 11:45 am
Even if it makes no difference here, it should read : IF Object_id('tempdb..#tmp') IS NOT NULL
With tempdb.. missing you'll never have a hit on that id.
November 2, 2010 at 11:53 am
Ninja's_RGR'us (11/2/2010)
Even if it makes no difference here, it should read : IF Object_id('tempdb..#tmp') IS NOT NULLWith tempdb.. missing you'll never have a hit on that id.
Hmmmm....
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
PRINT 'No tempdb. needed'
END;
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
PRINT 'tempdb. WAS needed'
END;
Hm, well shucks. Here's my failure point on realizing that... 😀
USE tempdb
GO
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
PRINT 'No tempdb. needed'
END;
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
PRINT 'tempdb. WAS needed'
END;
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
November 2, 2010 at 11:57 am
Craig Farrell (11/2/2010)
Ninja's_RGR'us (11/2/2010)
Even if it makes no difference here, it should read : IF Object_id('tempdb..#tmp') IS NOT NULLWith tempdb.. missing you'll never have a hit on that id.
Hmmmm....
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
PRINT 'No tempdb. needed'
END;
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
PRINT 'tempdb. WAS needed'
END;
Hm, well shucks. Here's my failure point on realizing that... 😀
USE tempdb
GO
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
PRINT 'No tempdb. needed'
END;
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
PRINT 'tempdb. WAS needed'
END;
:w00t::w00t::w00t:
Ok so temdb is not acutally required to be in the object_name :hehe:.
I'd love to see a proc like this :
use ssc
create #x (a int)
use tempdb
if object_id('#x') is not null
begin
use ssc
drop table #x
end
November 2, 2010 at 12:26 pm
Ninja's_RGR'us (11/2/2010)
Craig Farrell (11/2/2010)
Ninja's_RGR'us (11/2/2010)
Even if it makes no difference here, it should read : IF Object_id('tempdb..#tmp') IS NOT NULLWith tempdb.. missing you'll never have a hit on that id.
Hmmmm....
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
PRINT 'No tempdb. needed'
END;
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
PRINT 'tempdb. WAS needed'
END;
Hm, well shucks. Here's my failure point on realizing that... 😀
USE tempdb
GO
CREATE TABLE #tmp (tID INT);
IF Object_id('#tmp') IS NOT NULL
BEGIN
PRINT 'No tempdb. needed'
END;
IF Object_id('tempdb..#tmp') IS NOT NULL
BEGIN
PRINT 'tempdb. WAS needed'
END;
:w00t::w00t::w00t:
Ok so temdb is not acutally required to be in the object_name :hehe:.
I'd love to see a proc like this :
use ssc
create #x (a int)
use tempdb
if object_id('#x') is not null
begin
use ssc
drop table #x
end
Are you saying that OBJECT_ID('#tmp') returned a not null value for you? This little test returns a null for me.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 2, 2010 at 2:18 pm
Yes, but only when running the code in tempdb, that's why I was having a little fun with it in the code snippe with a bunch of usings.
November 3, 2010 at 9:58 am
But again, if there is a CREATE table it will give the same error as before right..?
USE SSC
CREATE #X (A INT)
USE TEMPDB
IF OBJECT_ID('#X') IS NOT NULL
BEGIN
USE SSC
DROP TABLE #X
CREATE #X (A INT) -- I mean this line
END
Thanks & Regards,
MC
November 3, 2010 at 12:18 pm
Of course, I was just having fun with the tempdb thingie.
Viewing 13 posts - 16 through 27 (of 27 total)
You must be logged in to reply to this topic. Login to reply