June 20, 2007 at 10:06 am
I'm creating a local temp table for storing data like this:
CREATE TABLE #fileexist ([File Exists] int, [File is a Directory] int, [Parent Directory Exists] int)
However, I often get an error stating "There is already an object named '#fileexist' in the database". Therefor, I would like to do a IF EXISTS statement prior to creating the table to to drop it and suppress any error message, like this:
IF EXISTS (SELECT whatever...)
DROP TABLE #fileexist
CREATE TABLE #fileexist ([File Exists] int, [File is a Directory] int, [Parent Directory Exists] int)
Is there a way to do something like this? The actual table is virtual and only is visible to the current scope (doesn't show up in tempdb). Thanks for any advice you can offer!
June 20, 2007 at 10:56 am
IF OBJECT_ID('tempdb..#TableExist') IS NOT NULL
TRUNCATE TABLE #TableExist
June 20, 2007 at 11:09 am
Thanks for your response. I'm getting some weird error messages. When I try to drop the table I get "Cannot drop the table '#fileexist', because it does not exist or you do not have permission" I am logged in as SA.
When I try to create the table I still get the error "There is already an object named '#fileexist' in the database".
So I'm getting contradicting messages. Any ideas? Thanks.
June 20, 2007 at 11:49 am
begin try
drop table #test
end try
begin catch
end catch
create table #test(c1 int)
June 21, 2007 at 12:13 am
IF OBJECT_ID('TempDB..#FileExist') IS NOT NULL
DROP TABLE #FileExist
Make sure the name of the table is correct.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply