1.資料庫
if exists(select 1 from master..dbo.sysdatabases where name='example')
print 'DataBase existed'
else
print 'Database not existed'
2.表
IF Exists(Select 1 From sysObjects Where Name ='表名' And Type In ('S','U'))
Print 'Exists Table'
Else
Print 'Not Exists Table'
2. 怎麼判斷sql資料庫是否存在,存在刪除
判斷資料庫,如果存在則刪除:
IF (EXISTS(SELECT * FROM master.dbo.sysdatabases WHERE dbid=db_ID('dbname')))
DROP DATABASE dbname
如果提示:刪除資料庫時提示資料庫正在被使用,無法刪除(Cannot drop database databasename because it is currently in use),使用:
IF (EXISTS(SELECT * FROM master.dbo.sysdatabases WHERE dbid=db_ID('dbname')))
BEGIN
USE master
ALTER DATABASE dbname
SET single_user
WITH ROLLBACK IMMEDIATE
DROP DATABASE dbname
(2)sql判斷資料庫表是否存在擴展閱讀
判斷儲存過程,如果存在則刪除
IF (EXISTS(SELECT * FROM sysobjects WHERE name='procerename' AND type='P'))
DROP PROCEDURE procerename
判斷觸發器,如果存在則刪除
IF (EXISTS(SELECT * FROM sysobjects WHERE id=object_id(N'[dbo].[triggername]') AND OBJECTPROPERTY(id, N'IsTrigger') = 1))
DROP TRIGGER triggername
判斷用戶函數是否存在,如果存在則刪除
此處type有兩種: 'TF'- Table-Value Function 表值函數 'FN'- Scalar-Value Function 標量值函數
IF (EXISTS(SELECT * FROM sysobjects WHERE id=object_id(N'[dbo].[userfunction]') AND (type='FN' OR type='TF')))
DROP FUNCTION userfunction
3. 如何判斷一個資料庫是否存在 (SQL Server 2008)
SqlConnection myconn=new SqlConnection("server=.;datebase=master;uid=sa;pwd=xxx");
myconn,Open();
string sql="select 1 from master,dbo.sysdatabases where [name]='要檢查的是否存在資料庫名字'「
SqlCommand cmd=new SqlCommand(sql,mycnn);
if(cmd.ExecuteScalar()!=null)
{
MessageBox.Show("已存在該資料庫");
}
上面是c#的寫法,但是解決方案大致就是,創建master資料庫的連接,執行
select 1 from master,dbo.sysdatabases where [name]='要檢查的是否存在資料庫名字'
是否有返回值就是有了反之就沒有
更簡單的就是全盤收索(或者你知道範圍的去限定搜索范圍)
要檢查的是否存在資料庫名字.mdf
這個文件吧有就是有的話一般就是有這個資料庫了,不過也存在沒有附加的可能,但是沒有這個文件就說明絕對是沒有這個資料庫的
4. sql語言如何判斷資料庫中有表
use bbsDB
if exists (select * from sysobjects where name = 'bbsUsers')
drop table bbsUsers
go
create table bbsUsers
(
uID int identity(1,1) not null,
uName nvarchar(15) not null,
uPassWord varchar(10) not null,
uEmail varchar(20) not null,
uSex int not null,
uClass int null,
uRemark varchar(20) null,
uRegDate smalldatetime not null,
uState int null,
uPoint int null,
)
go
這是判斷並建bbsUsers表,我昨天剛學的。
5. 如何判斷SQL中某個資料庫是否存在
在SQL Server資料庫編程時,常常需要判斷一個資料庫是否已經存在,如果不存在則創建此資料庫。常用的方法有以下三種:
1. select * From master.dbo.sysdatabases where name='test_db'
如果不存在查詢結果,則說明name所表示的資料庫不存在
2. object_id('test_db')
如果無法獲取對象ID(null),則說明此對象不存在;常用
if object_id('test_db') is null
或者
if (select object_id('test_db')) is null
3. db_id('test_db')
如果不能獲取資料庫ID,則說明name所表示的資料庫不存在;實際上此種方法也是在sysdatabases中查找,並返回資料庫的ID;常用
if db_id('test_db') is null
或者
if (select db_id('test_db')) is null
6. SQL求助:想創建一個表,但在創建前先判斷是否存在。如存在就刪除它,不存在就創建。
1、創建對象時,如果沒有指定Schema,就會掛在默認的Schema dbo下面。
7. 在SQL中怎麼判斷資料庫里是否存在一張表
'select * from '+tableName+' where 1=0'
8. [轉載]java和sql如何判斷資料庫表是否存在
1.sql語句判斷資料庫表是否存在: sql:select * from user_all_tables where table_name='tableName' String helperName= delegator.getGroupHelperName("com.asiainfo"); SQLProcessor sqlProcessor= new SQLProcessor(helperName); String sql = "select * from user_all_tables where table_name='"+table+"'"; ResultSet rsTables =sqlProcessor.executeQuery(sql); if(rsTables.next()){ Debug.logWarning("table:"+table+"exists", mole);}else{ Debug.logWarning("table:"+table+" does not exist", mole);}方法二:DatabaseMetaData meta = m_sqlCon.getMetaData(); ResultSet rsTables = meta.getTables(null , null, 「YourTableName」, null); if(rsTables.next()){ System.out.println("The Table exsits.");}else{ System.out.println("The 如果schema參數為null的話,那麼它會查詢整個資料庫中的表有可能會沖突的: getTables(String catalog,String schemaPattern,String tableNamePattern,String[] types) 參數: catalog:目錄名稱,一般都為空. 參數:schema:資料庫名,對於oracle來說就用戶名 參數:tablename:表名稱 參數:type :表的類型(TABLE | VIEW) 注意:在使用過程中,參數名稱必須使用大寫的。
9. 怎樣可以檢測SQL資料庫中某個表是否存在
在sql資料庫中有一個sysobjects表,記錄當前所有的表名可以用Query(SQL語句)SELECT * FROM sysobjectsWHERE '表名' = Name或用Table打開,用Locate找一下