如何判斷資料庫是否存在

2022-07-20 09:36:10 字數 4465 閱讀 8325

在建立資料庫時,經常要判斷伺服器中是否存在某個資料庫,然後再決定是不是要進行下一步操作。

以下是一些簡單地示例,希望會有用。

2:

ifexists(select * from master.dbo.sysdatabases where name = 'skybusiness')

3:

begin

4:

drop

database skybusiness

5:

print

'skybusiness 已經存在,已被刪除'

6:

end

7:

else

8:

begin

9:

create

database skybusiness

10:

onprimary

11:         (
12:           name=skybusiness_mdf,
13:           filename='c:\skybusiness.mdf',
14:

size=10mb,

15:           maxsize=50mb,
16:           filegrowth=25%
17:         )
18:         log on
19:         (
20:           name=skybusiness_ldf,
21:           filename='c:\skybusiness.ldf',
22:

size=10mb,

23:           maxsize=50mb,
24:           filegrowth=25%
25:         )
26:

print

'skybusiness資料庫建立成功'

27:

end

28:
29: --建聚集索引
30:

create

clustered

index index_yexinwinners

31:

on tablename(column_name)

32:

with

fillfactor = 10

33:
34: --建非聚集索引
35:

create

nonclustered

index index_yexinwinners

36:

on tablename(column_name)

37:

with

fillfactor = 10

38:
39: --建檢視
40:

create

view view_name

41:

as

42:

select * from pubs.titles(sql語句,任意)

43:
44: --建檢查檢視
45:

create

view view_name

46:

as

47:

select * from pubs.titles(sql語句,任意)

48:

with

check

option

49:
50: --建加密檢視
51:

create

view view_name

52:

with encryption

53:

as

54:

select * from pubs.titles(sql語句,任意)

55:
56: --建表
57:

create

table tablename

58: ( ---(欄位名,字段型別 自己任意)
59: stuid int

notnull

identity(1,1) primary

key,

60: stuname varchar(30) not

null,

61: stuage char(3) not

null

62: )
63:
64: --新增check約束
65:

alter

table tablename

66:

addcheck(stuage >0 and stuage <100)

67:
68: --新增外來鍵
69:

alter

table tablename

70:

addforeign

key (stuname)

71:

references xtablename(stuname)

72:
73: --新增唯一約束
74:

alter

table tablename

75:

addunique(stuid)

76:
77: ---建事務
78:

begin tranaction

79:

update xtable set money = money + 10000 where id = 1

80:

update xtable set money = money - 10000 where id = 3

81:

if(@@error <> 0)

82:

begin

83:

print

'轉帳失敗'

84:

rollback

transaction

85:

end

86:

else

87:

begin

88:

print

'轉帳成功'

89:

commit

transaction

90:

end

91:
92: --建游標
93:

declare cursor_name cursor

94:

forselect * from northwind.product

95:
96: --建更新游標
97:

declare cursor_name cursor

98:

forselect * from northwind.product

99:

forupdate

100:
101: --建隨意移動游標
102:

declare cursor_name cursor

scroll

103:

forselect * from northwind.product

104:
105: --使用游標
106:

open cursor_name

107:
108: --關閉游標
109:

close cursor_name

110:
111: --刪除游標
112:

deallocate cursor_name

113:
114: --移動游標
115:
116:

fetch

next

-- 下一條記錄

117:

fetch

last --最後一條記錄

118:

fetch

first --第一條記錄

119:

fetch

prior --上一條記錄

120:

fetch

absolute n -- 絕對位置

sql server如何判斷資料庫是否存在

如何判斷資料庫是否存在 執行下列的sql,獲得一張表,根據表的行數來判斷。select from master.sysdatabases where name n 所查詢的資料庫名 if exists select from master.dbo.sysdatabases where name ye...

判斷資料庫中表是否存在

sql server if exist select from sysobjects where id object id table1 then drop table1 if exists select top 1 id from tablename1 用 if exists select 1 f...

如何判斷資料庫,表或字段是否存在

在新增新的資料庫,表或字段的時候,新增之前一般都會檢查是否已經存在,這樣做的好處是保證指令碼的穩定性,再次執行的時候也不會報錯了。有兩種方法,一種是使用內建的函式,另外一種是查詢系統表,總結的sql指令碼如下。1 usemaster 2go 34 判斷資料庫是否存在5 方法1 使用函式db id6i...