Sql Server中判斷表或者數據庫是否存在sql
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'對象
Sql Server中判斷表或者數據庫是否存在 分類:軟件開發2007.8.12 14:31 做者:stone | 評論:1 | 閱讀:2996 1.數據庫 if exists(select 1 from master..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'string
在Sql Server2005中能夠簡化上述語句table
如:ast
use example擴展
go
if object_id('a','U') is not null
drop table a
go
注:a 是一個表,U表明是數據表類型
相似於U的類型代碼,以下所示
對象類型:
AF = 聚合函數 (CLR)
C = CHECK 約束
D = DEFAULT(約束或獨立)
F = FOREIGN KEY 約束
PK = PRIMARY KEY 約束
P = SQL 存儲過程
PC = 程序集 (CLR) 存儲過程
FN = SQL 標量函數
FS = 程序集 (CLR) 標量函數
FT = 程序集 (CLR) 表值函數
R = 規則(舊式,獨立)
RF = 複製篩選過程
SN = 同義詞
SQ = 服務隊列
TA = 程序集 (CLR) DML 觸發器
TR = SQL DML 觸發器
IF = SQL 內聯表值函數
TF = SQL 表值函數
U = 表(用戶定義類型)
UQ = UNIQUE 約束
V = 視圖
X = 擴展存儲過程
IT = 內部表
一條sql語句判斷數據是否存在 string sql = @" IF EXISTS (SELECT * FROM [表] WHERE [name]='tom') UPDATE [表] SET [age]=20 WHERE [name]='tom' ELSE BEGIN INSERT INTO [表] ([name],[age],[sex]) VALUES ('tom',20,'male') END ";