How to check if a table already exists 
Author Message
 How to check if a table already exists
Hello All,

Is there any system stored procedure which can tell if a table already
exists in the db?

I know how to do this by a query ... but that makes code unreadable.

regards,
Abhishek.



Thu, 26 Jan 2006 13:21:30 GMT
 How to check if a table already exists

  I think that running a query against the information_schema views should
be fine,   but you can also use the OBJECT_ID ('TableName') function.  If it
returns NULL, then the object doesn't exist

Adi

Quote:
> Hello All,

> Is there any system stored procedure which can tell if a table already
> exists in the db?

> I know how to do this by a query ... but that makes code unreadable.

> regards,
> Abhishek.



Thu, 26 Jan 2006 14:58:18 GMT
 How to check if a table already exists
IF OBJECT_ID('dbo.YourTable') IS NOT NULL
    -- Do your thing

--

Andrew J. Kelly
SQL Server MVP


Quote:
> Hello All,

> Is there any system stored procedure which can tell if a table already
> exists in the db?

> I know how to do this by a query ... but that makes code unreadable.

> regards,
> Abhishek.



Thu, 26 Jan 2006 14:03:07 GMT
 How to check if a table already exists
Be aware that Object_ID() only returns a status whether or not an object
exists.

e.g.
create proc myTable
as
select 1
go

--false info
if object_id('myTable') is not null
print 'False: myTable exists'

--using undoc second parameter
if object_id('myTable','U') is not null
print 'myTable exists'
go

drop proc myTable
go

--
-oj
Rac v2.2 & QALite
http://www.rac4sql.net


Quote:
> Hello All,

> Is there any system stored procedure which can tell if a table already
> exists in the db?

> I know how to do this by a query ... but that makes code unreadable.

> regards,
> Abhishek.



Thu, 26 Jan 2006 21:46:00 GMT
 How to check if a table already exists
Hi Abhishek,

You can check existance of table by querying Sysobjects system table.

if exists (select * from sysobjects where id =
object_id(N'[dbo].[Contacts]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)
  Print 'Table Exists'
GO

By using this query.

Regards
Hari Sharma, India

Quote:

> Hello All,

> Is there any system stored procedure which can tell if a table already
> exists in the db?

> I know how to do this by a query ... but that makes code unreadable.

> regards,
> Abhishek.



Fri, 27 Jan 2006 11:10:46 GMT
 
 [ 5 post ] 

 Relevant Pages 

1. vb: check if table already exists??

2. check if table already exists??

3. How to check if an tempory table already exists

4. Checking if a #temp1 table exists already

5. How do I check if a table already exists?

6. How to check if Temp SP already exist

7. Check if cursor already exists.

8. Checking if a Global Variables already exists

9. how to check if a cursor already exists...

10. Check if temp. SP already exists

11. Check if a file already exists

12. Temporary Table already exists


 
Powered by phpBB® Forum Software