Determine whether a table or database exists in SQL Server

Source: Internet
Author: User

SQL Server determine whether a table or database exists in the

SQL Server determine if the database exists:
Law (i):

SELECT * from master.dbo.sysdatabases where name= ' database name '

Law (ii):
If DB_ID (' database name ') is Notnull

Drop Database ...
Go

Create ...

SQL Server To determine whether the Table object exists:
Select COUNT (*) from sysobjects Whereid = object_id (' Database name '. Owner. Table name ')

if exists

(SELECT COUNT (*) from sysobjects where id = object_id (' database name. Owner. Table name '))
print ' exists '
Else
print ' does not exist '

SQL Server determine whether a field exists in the table:
if exists

(SELECT * from syscolumns where name= ' colname1 ' and id=object_id (' database name. Owner. Table name '))
print ' exists '
Else
print ' does not exist '
(Represents the Colname1 field in table TableName1)
Cases:
SELECT * from syscolumns wherename= ' Test ' and id=object_id (' Dbo.test ')

SQL Server To determine whether a stored procedure or view exists:

If object_id (' View or stored procedure name ') is not null
Drop Proc/view ...
Go

Create Proc/view ...

Or

If Exists (select * from sysobjects where name= ' view or stored procedure name ' and type = ' p/v ')
Drop Proc/view ...
Go

Create Proc/view ...

Determine whether a table or database exists in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.