Re: Checking if a table exists, so that it may be dropped before recreating it
From: Gregory A. Larsen (greg.larsen_at_netzero.com)
Date: 05/06/04
- Next message: me_at_privacy.net: "How to do this from DTS?"
- Previous message: Gregory A. Larsen: "Re: Adding Preceeding zeros"
- In reply to: Dr Tarheel: "Checking if a table exists, so that it may be dropped before recreating it"
- Next in thread: Trey Walpole: "Re: Checking if a table exists, so that it may be dropped before recreating it"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 6 May 2004 14:09:43 -0700
This is the method SQL Server uses when it generates scripts. Seems fairly
straight forward to me.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[a]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[a]
GO
CREATE TABLE [dbo].[a] (
[a] [int] NOT NULL
) ON [PRIMARY]
GO
-- ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- ---- Need SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples "Dr Tarheel" <anonymous@discussions.microsoft.com> wrote in message news:615F961F-D6A8-42D2-AB59-58584CB0B122@microsoft.com... > It may be argued that truncating the original and running an INSERT would be more efficient, but I am required to create the table afresh each time. There will be some occasions where the table may be already dropped. If that is the case, I'd like to skip the DROP TABLE command and go straight to the SELECT. I understand I can query sysobjects to determine if the table exists, but what is the most straight forward way to implement this. A code snippet would be a world of help. > > Thanks, > Dr Tarheel
- Next message: me_at_privacy.net: "How to do this from DTS?"
- Previous message: Gregory A. Larsen: "Re: Adding Preceeding zeros"
- In reply to: Dr Tarheel: "Checking if a table exists, so that it may be dropped before recreating it"
- Next in thread: Trey Walpole: "Re: Checking if a table exists, so that it may be dropped before recreating it"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|