Check if a database or table exists using Sql Server 2005
2008-04-02 13:42
761 查看
The
Object_ID() takes in the object name and object type as parameters. The
object name is the object used and the object type is the type of
object used in a schema.
For example to check if a table exists in a database, use this query :
IF OBJECT_ID ('AdventureWorks.dbo.AWBuildVersion','U') IS NOT NULL
Print 'Table Exists'
ELSE
Print 'Table Does Not Exists'
where 'AdventureWorks.dbo.AWBuildVersion' is the object name and 'U' is the object type which represents a table
Similarly
you can check for a stored procedure or a view by specifying the
correct object type. You can get an entire list of object types over here.
To check if a database exists, you can use the DB_ID() function as shown below :
IF db_id('AdventureWorks') IS NOT NULL
Print 'Database Exists'
ELSE
Print 'Database Does Not Exists'
相关文章推荐
- add column for a table if this column not exists using T-SQL in SQLServer
- SqlServer IF Exists([database]|[table]|[prop]) / Column([Operation])
- DROP TABLE IF EXISTS” in Oracle and SQL Server
- Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler--转载
- How to check if a date is a valid date in Sql Server 2005
- Copy a table from one database to another in SQL Server 2005
- DROP TABLE IF EXISTS” in Oracle and SQL Server
- Paging Records Using SQL Server 2005 Database - ROW_NUMBER Function
- “DROP TABLE IF EXISTS” in Oracle and SQL Server
- Restore SQL Server 2005 error : "The tail of the log for the database "TEST1" has not been backed up."
- SQL logic error or missing database no such table: xxx
- Using the INSERTED table in SQL Server 2000
- Using Plan Guides in SQL Server 2005
- MCITP SQL Server 2005 Database Developer All-in-One Exam Guide
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过
- SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
- Using Visual Studio 2005 to Perform Load Testing on a SQL Server 2005 Reporting Services Report Server
- Using NOLOCK and READPAST table hints in SQL Server
- Beginners Guide to SQL Server Integration Services Using Visual Studio 2005
- PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.semaphore' doesn't