May 16, 2008 at 11:03 am
Here is what I am doing and obviously it never does the Else if the table does not exist with rows in it!!
If exists (select top 1 * from ##tmp_mj_jobSync)
Drop table ##tmp_mj_jobSync
Else
Select * into ##tmp_mj_jobSync from mj_jobSync
What can I replace "If exists (select top 1 * from ##tmp_mj_jobSync)" so that the Else statement will run????????????
May 16, 2008 at 11:10 am
This?
If exists (select top 1 * from ##tmp_mj_jobSync)
Drop table ##tmp_mj_jobSync
Select * into ##tmp_mj_jobSync from mj_jobSync
Actually, there is something better, but I have to find it... Stay Tune.
😎
May 16, 2008 at 11:16 am
Thanks Lynn. Yes I know there is something better and I am looking also. So I definitely will stay tuned.
May 16, 2008 at 11:22 am
This is the way you should do it:
if exists(select object_id(N'tempdb.dbo.##tmp_mj_jobSync'))
drop table ##tmp_mj_jobSync
Select * into ##tmp_mj_jobSync from mj_jobSync
😎
May 16, 2008 at 11:48 am
You should avoid using global temp tables (##tablename), use local temp tables (#tablename).
This should a better way to check for a temp table. The other methods will have a problem if ##tmp_mj_jobSync is not a table, but some other temp object type, like a procedure.
if object_id(N'tempdb..#tmp_mj_jobSync','U') is not null
drop table #tmp_mj_jobSync
Select * into #tmp_mj_jobSync from mj_jobSync
May 16, 2008 at 8:41 pm
I use the same method Michael, posted, as well. Works in 2k and 2k5 equally as well and avoids dipping system tables.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 17, 2008 at 5:56 pm
BEGIN TRY
drop table ##tmp_mj_jobSync
END TRY BEGIN CATCH END CATCH
Select * into ##tmp_mj_jobSync from mj_jobSync
This works also... 🙂
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply