Query to Check SP/Table/Trigger Exists in Database or not
We often need create/update Stored Procedure,Tables or Triggers in database via SQL scripts. It is a best practice to check if these objects exist in the database before create/update them.
Let's look at several examples
1. Checking whether procedure exist in the NORTHWIND database with name 'CustOrdersDetail'
Approach -1:use NORTHWIND
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CustOrdersDetail]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
print 'CustOrdersDetail Exists in the NORTHWIND database'
else
print 'CustOrdersDetail *does not exist* in the NORTHWIND database'
Approach -2:
Use NORTHWIND
if exists(select * from dbo.sysobjects where type = 'p' and name = 'CustOrdersDetail’ )
print ''CustOrdersDetail Exists in the NORTHWIND database'
else
print ''CustOrdersDetail *DOES not exist* in the NORTHWIND database'
2. Checking whether table exist in the database with name 'EmployeeTerritories'
Approach -1:
use NORTHWIND
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EmployeeTerritories]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'EmployeeTerritories Exists in the NORTHWIND database'
else
print 'EmployeeTerritories *DOES not exist* in the NORTHWIND database'
Approach -2:
use NORTHWIND
if exists(select * from dbo.sysobjects where type = 'U' and name = 'EmployeeTerritories' )
print 'EmployeeTerritories Exists in the NORTHWIND database'
else
print 'EmployeeTerritories *DOES not exist* in the NORTHWIND database'
3. Checking whether trigger exist in the database with name 'TempTrigger'
Approach -1:
use Northwind
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TempTrigger]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
print 'TempTrigger Exists in the NORTHWIND database'
else
print 'TempTrigger *DOES not exist* in the NORTHWIND database'
Approach -2:
use Northwind
if exists (select * from dbo.sysobjects where name ='TempTrigger' and type ='TR')
print 'TempTrigger Exists in the NORTHWIND database'
else
print 'TempTrigger *DOES not exist* in the NORTHWIND database'
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1038635/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- flashback drop/query/table/database/archiveDatabaseHive
- Check database status in RACDatabase
- How to Perform a Health Check on the DatabaseORMDatabase
- restore database check readonlyRESTDatabase
- How to check Database corrupt BlockDatabaseBloC
- check database patch with opatch toolsDatabase
- create table if not exists Waiting for table metadata lockAI
- Database Logoff Trigger SQLDatabaseGoSQL
- Database | 淺談Query Optimization (1)Database
- Database | 淺談Query Optimization (2)Database
- create table of mysql databaseMySqlDatabase
- Kettle Table Exists控制元件優化控制元件優化
- MySQL :Ignoring query to other databaseMySqlDatabase
- beego報錯 table name: `xxx` not existsGo
- 【DB】Direct Path EXP Corrupts The Dump If An Empty Table Partition Exists
- How to check whether the current database in using Oracle optionsDatabaseOracle
- ORA-01031 CREATE TRIGGER ON DATABASEDatabase
- PRVF-7617 Cluster Verify Fails For Network Check if Firewall Exists_1357657.1AI
- 使用Trigger實現兩個Table同步更新資料
- OGG Replicat Failed Due To Check_point Table beingTruncatedAI
- logon on database記錄登入資訊的triggerGoDatabase
- 閃回之 回收站、Flashback Drop (table、index、trigger等)Index
- 系統許可權ADMINISTER DATABASE TRIGGER的作用Database
- check whether the crystal report runtime is exists 檢查crystalreport執行時是否存在
- android.database.sqlite.SQLiteException: no such table錯誤AndroidDatabaseSQLiteException
- SQL SERVER隱藏系統函式sp_columns_100_rowset和sp_table_statistics2_rowsetSQLServer函式
- django資料庫同步時報錯“Table 'XXX' already exists”Django資料庫
- Oracle資料泵impdp的table_exists_action引數說明Oracle
- impdp匯入時使用table_exists_action引數的區別
- 【匯出匯入】IMPDP table_exists_action 引數的應用
- MySQL資料庫出現 Ignoring query to other databaseMySql資料庫Database
- [Oracle] exists 和 not existsOracle
- SP2-0618: Cannot find the Session Identifier.Check PLUSTRACE role is enabledSessionIDE
- SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabledSessionIDE
- SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabledSessionIDE
- [20221227]a mutating table error without a trigger!.txtError
- MySQL 5.6 drop database時,table metadata lock等待MySqlDatabase
- Flashback database與flashback table使用條件區別Database