tags:

views:

2926

answers:

7

I have a DTS package that drops a table then creates it and populates it but sometimes something happens and the package fails after the drop table. If it's rerun it fails cuz the table hasn't been created yet.

Is there something like "if exists" for SQLServer 2000 like in MySQL?

thanks.

Edit Thanks everyone! I went with TrickyNixon because his was quick and easier to initiate. But, I would like everyone to eval that statement and make sure it is best-practices because if it is, I'm TOTALLY going to dump that into EVERY drop/create DTS package I see!!! Nice work TrickyNixon and everyone who threw in on this.

+9  A: 
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))
DROP TABLE TableName;
GO

You can check a list of type definitions in the sys.objects table here if you want to check if other objects in your database exist.

Brian R. Bondy
Dang, you beat me to it!
Mitchel Sellers
In the question though keng specifically mentioned SQL Server 2000. I thought sys.objects was introduced in SQL Server 2005 and for 2000 you need to use dbo.sysobjects
Clive
@Clive: Did not know that. I will leave the answer though because many people will find it that are using other SQL Server versions.
Brian R. Bondy
@Brian, sure no worries. We had some issues at work because we had SQL Server 2000 in production but were using SQL Server 2005 in 2000 compatibility mode in development (ahead of upgrading production). By default if you generated scripts from the 2005 database the drop statements used sys.objects, but these scripts then would fail on the 2000 database. Using dbo.sysobjects worked for both 2000 and 2005 versions. If you still need to use 2000 I would suggest using dbo.sysobjects, if you are on newer versions then go with sys.objects.
Clive
Found this via Google, but it gives an error for me in SQL Server 2008. I think that either the END shouldn't be there, or else there should be a corresponding BEGIN.
Mark Byers
@Mark: Thanks, fixed. The `END` was left over from a Create table statement after the drop from my project reference code.
Brian R. Bondy
+1  A: 

You need to check the sysobjects table

Michał Piaskowski
+1  A: 

The following works, just replace TABLENAME with your table

IF EXISTS( SELECT * FROM dbo.sysobjects where id = object_id(N'TABLENAME') AND OBJECTPROPERTY(id, N'IsTable') = 1)
BEGIN
    DROP TABLE TABLENAME
END
Mitchel Sellers
Even shorter, skip the call to Sysobjects...IF OBJECTPROPERTY(object_id(N'TableName'), N'IsTable') = 1BEGIN DROP TABLE TableNameEND
Kevin Fairchild
Very good point!
Mitchel Sellers
Also, you left out the single-quote after IsTable
Kevin Fairchild
Thanks Kevin, fixed it....
Mitchel Sellers
+9  A: 

Or quicker:

IF OBJECT_ID('temp_ARCHIVE_RECORD_COUNTS') IS NOT NULL  
  DROP TABLE temp_ARCHIVE_RECORD_COUNTS
TrickyNixon
man I'm all for easy!!
Keng
Although if you have a stored procedure or some other object, this will lead to an SQL error.
Brian R. Bondy
IF you want to verify the object passed to object_id is in fact a user table, there's a 2nd parameter to the function:IF OBJECT_ID('temp_ARCHIVE_RECORD_COUNTS', 'U') IS NOT NULL DROP TABLE temp_ARCHIVE_RECORD_COUNTSI believe this was undocumented but worked in 2000, and now is documented.
Jeffrey Meyer
+2  A: 

Sure:

IF OBJECT_ID('YOURTABLENAME') IS NOT NULL

where YOURTABLENAME is whatever the name of your table is.

If it's a temp table, then just add tempdb.# before before the OBJECT_ID function call.

+2  A: 

One thing to remember when you drop and object and then add back to the database is also add any permissions back to the table. This has tripped us up a number of times.

I up voted TracyNixon's answer. I would say you want to stay away from querying the sysobjects table directly because a Microsoft update could break that kind of code. You isolate yourself from that by using the OBJECT_ID function.

Mike Daniels
+2  A: 

Noone has mentioned this method yet:

if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME='MyTable') begin 
    drop table MyTable
end

This is the most portable method - it works on at least MSSQL2000 up to MSSQL2008.

The INFORMATION_SCHEMA tables are part of the SQL-92 standard.

Blorgbeard