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


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


Relevant Pages

  • Re: SQL 2005 Full Text Multi-Column Ranking
    ... > I looked all over the internet and can't find a straight answer, ... In SQL Server 2005 you can provide a column list instead of a single column ... Prev by Date: ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Printing my Stored Procedures
    ... It is pretty straight forward. ... Vyas, MVP (SQL Server) ...
    (microsoft.public.sqlserver.server)
  • Re: Dynamic Connection
    ... Same no-experience about Oracle here, but for MS SQL Server, that is, ... indeed, quite straight forward. ... Bringing the mda may be a good idea, it may re-assure him / her! ...
    (microsoft.public.access.modulesdaovba)
  • RE: Security Problem - Permission denied
    ... Nevermind, I found the answer to my problem in another post. ... SQL Server security seemed pretty straight ...
    (microsoft.public.sqlserver.security)
  • Re: looking for a random function
    ... > random positive integer number, say, between 1 and n, n being a positive ... > int nrand ... I think your algorithm can be expressed a little more naturally as: ... rather than a straight line. ...
    (comp.programming)