SQL判斷是否存在

2021-09-02 23:58:48 字數 1796 閱讀 3435

1

判斷資料庫是否存在

2 ifexists(select

*frommaster..sysdatabaseswherename=

n』庫名』)

3print』exists』4

else

5print

』notexists』67

---------------8--

判斷要建立的表名是否存在

9 ifexists(select

*fromdbo.sysobjectswhereid=

object_id(n』[

dbo].[

表名]』)andobjectproperty(id,n』isusertable』)=1)

10--

刪除表11 droptable[

dbo].[表名]

12go

1314

---------------

15--

判斷要建立臨時表是否存在

16ifobject_id(』tempdb.dbo.#test』)isnotnull

17begin

18print

』存在』

19end

20else

21begin

22print

』不存在』

23end

2425

---------------

26--

判斷要建立的儲存過程名是否存在

27 ifexists(select

*fromdbo.sysobjectswhereid=

object_id(n』[

dbo].[

儲存過程名

]』)andobjectproperty(id,n』isprocedure』)=1)

28--

刪除儲存過程

29 dropprocedure[

dbo].[

儲存過程名]30

go31

32--

-------------

33--

判斷要建立的檢視名是否存在

34 ifexists(select

*fromdbo.sysobjectswhereid=

object_id(n』[

dbo].[

檢視名]』)andobjectproperty(id,n』isview』)=1)

35--

刪除檢視

36 dropview[

dbo].[

檢視名]

37go

3839

---------------

40--

判斷要建立的函式名是否存在

41 ifexists(select

*fromdbo.sysobjectswhereid=

object_id(n』[

dbo].[

函式名]』)andxtypein(n』fn』,n』if

』,n』tf』))

42--

刪除函式

43 dropfunction[

dbo].[

函式名]

44go

4546 ifcol_length(』表名』,』列名』)isnull

47print

』不存在』

4849 select1fromsysobjectswhereidin(selectidfromsyscolumnswherename=』列名』)andname=

』表名』

5051

>

SQL判斷是否存在

判斷資料庫是否存在 if exists select from master.sysdatabases where name n 庫名 print exists else print not exists 判斷要建立的表名是否存在 if exists select from dbo.sysobjec...

sql判斷表 字段是否存在

mysql 1 判斷乙個表是否存在 語法 select table name from information schema.tables where table name 表名 sql例子 select table name from information schema.tables where...

SQL判斷資料表是否存在

最近重新做開發,又自己動手寫了sql語句,所以把一些東西記錄到這裡,為了加深印象,大家一起交流。假設有一張表,名為 personale if exists select from sysobjects where object id n personale and objectproperty id...