sql璇彞
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql璇彞相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/format' title='format'>format
杩炴帴 灞€鍩熺綉 test outer ota tsql 鐜板湪 flow1銆佽鏄庯細鍒涘缓鏁版嵁搴?/p>
CREATE DATABASE database-name
2銆佽鏄庯細鍒犻櫎鏁版嵁搴?/span>
drop database dbname
3銆佽鏄庯細澶囦唤sql server
--- 鍒涘缓 澶囦唤鏁版嵁鐨?device
USE master
EXEC sp_addumpdevice 鈥榙isk鈥? 鈥榯estBack鈥? 鈥榗:mssql7backupMyNwind_1.dat鈥?/span>
--- 寮€濮?澶囦唤
BACKUP DATABASE pubs TO testBack
4銆佽鏄庯細鍒涘缓鏂拌〃
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
鏍规嵁宸叉湁鐨勮〃鍒涘缓鏂拌〃锛?/span>
A锛歝reate table tab_new like tab_old (浣跨敤鏃ц〃鍒涘缓鏂拌〃)
B锛歝reate table tab_new as select col1,col2… from tab_old definition only
5銆佽鏄庯細鍒犻櫎鏂拌〃
drop table tabname
6銆佽鏄庯細澧炲姞涓€涓垪
Alter table tabname add column col type
娉細鍒楀鍔犲悗灏嗕笉鑳藉垹闄ゃ€侱B2涓垪鍔犱笂鍚庢暟鎹被鍨嬩篃涓嶈兘鏀瑰彉锛屽敮涓€鑳芥敼鍙樼殑鏄鍔爒archar绫诲瀷鐨勯暱搴︺€?/span>
7銆佽鏄庯細娣诲姞涓婚敭锛?Alter table tabname add primary key(col)
璇存槑锛氬垹闄や富閿細 Alter table tabname drop primary key(col)
8銆佽鏄庯細鍒涘缓绱㈠紩锛歝reate [unique] index idxname on tabname(col….)
鍒犻櫎绱㈠紩锛歞rop index idxname
娉細绱㈠紩鏄笉鍙洿鏀圭殑锛屾兂鏇存敼蹇呴』鍒犻櫎閲嶆柊寤恒€?/span>
9銆佽鏄庯細鍒涘缓瑙嗗浘锛歝reate view viewname as select statement
鍒犻櫎瑙嗗浘锛歞rop view viewname
10銆佽鏄庯細鍑犱釜绠€鍗曠殑鍩烘湰鐨剆ql璇彞
閫夋嫨锛歴elect * from table1 where 鑼冨洿
鎻掑叆锛歩nsert into table1(field1,field2) values(value1,value2)
鍒犻櫎锛歞elete from table1 where 鑼冨洿
鏇存柊锛歶pdate table1 set field1=value1 where 鑼冨洿
鏌ユ壘锛歴elect * from table1 where field1 like ’%value1%’ ---like鐨勮娉曞緢绮惧锛屾煡璧勬枡!
鎺掑簭锛歴elect * from table1 order by field1,field2 [desc]
鎬绘暟锛歴elect count as totalcount from table1
姹傚拰锛歴elect sum(field1) as sumvalue from table1
骞冲潎锛歴elect avg(field1) as avgvalue from table1
鏈€澶э細select max(field1) as maxvalue from table1
鏈€灏忥細select min(field1) as minvalue from table1
1銆佽鏄庯細鍒涘缓鏁版嵁搴?/p>
CREATE DATABASE database-name
2銆佽鏄庯細鍒犻櫎鏁版嵁搴?/span>
drop database dbname
3銆佽鏄庯細澶囦唤sql server
--- 鍒涘缓 澶囦唤鏁版嵁鐨?device
USE master
EXEC sp_addumpdevice 鈥榙isk鈥? 鈥榯estBack鈥? 鈥榗:mssql7backupMyNwind_1.dat鈥?/span>
--- 寮€濮?澶囦唤
BACKUP DATABASE pubs TO testBack
4銆佽鏄庯細鍒涘缓鏂拌〃
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
鏍规嵁宸叉湁鐨勮〃鍒涘缓鏂拌〃锛?/span>
A锛歝reate table tab_new like tab_old (浣跨敤鏃ц〃鍒涘缓鏂拌〃)
B锛歝reate table tab_new as select col1,col2… from tab_old definition only
5銆佽鏄庯細鍒犻櫎鏂拌〃
drop table tabname
6銆佽鏄庯細澧炲姞涓€涓垪
Alter table tabname add column col type
娉細鍒楀鍔犲悗灏嗕笉鑳藉垹闄ゃ€侱B2涓垪鍔犱笂鍚庢暟鎹被鍨嬩篃涓嶈兘鏀瑰彉锛屽敮涓€鑳芥敼鍙樼殑鏄鍔爒archar绫诲瀷鐨勯暱搴︺€?/span>
7銆佽鏄庯細娣诲姞涓婚敭锛?Alter table tabname add primary key(col)
璇存槑锛氬垹闄や富閿細 Alter table tabname drop primary key(col)
8銆佽鏄庯細鍒涘缓绱㈠紩锛歝reate [unique] index idxname on tabname(col….)
鍒犻櫎绱㈠紩锛歞rop index idxname
娉細绱㈠紩鏄笉鍙洿鏀圭殑锛屾兂鏇存敼蹇呴』鍒犻櫎閲嶆柊寤恒€?/span>
9銆佽鏄庯細鍒涘缓瑙嗗浘锛歝reate view viewname as select statement
鍒犻櫎瑙嗗浘锛歞rop view viewname
10銆佽鏄庯細鍑犱釜绠€鍗曠殑鍩烘湰鐨剆ql璇彞
閫夋嫨锛歴elect * from table1 where 鑼冨洿
鎻掑叆锛歩nsert into table1(field1,field2) values(value1,value2)
鍒犻櫎锛歞elete from table1 where 鑼冨洿
鏇存柊锛歶pdate table1 set field1=value1 where 鑼冨洿
鏌ユ壘锛歴elect * from table1 where field1 like ’%value1%’ ---like鐨勮娉曞緢绮惧锛屾煡璧勬枡!
鎺掑簭锛歴elect * from table1 order by field1,field2 [desc]
鎬绘暟锛歴elect count as totalcount from table1
姹傚拰锛歴elect sum(field1) as sumvalue from table1
骞冲潎锛歴elect avg(field1) as avgvalue from table1
鏈€澶э細select max(field1) as maxvalue from table1
鏈€灏忥細select min(field1) as minvalue from table1
11銆佽鏄庯細鍑犱釜楂樼骇鏌ヨ杩愮畻璇?/span>
A锛?UNION 杩愮畻绗?/span>
UNION 杩愮畻绗﹂€氳繃缁勫悎鍏朵粬涓や釜缁撴灉琛紙渚嬪 TABLE1 鍜?TABLE2锛夊苟娑堝幓琛ㄤ腑浠讳綍閲嶅琛岃€屾淳鐢熷嚭涓€涓粨鏋滆〃銆傚綋 ALL 闅?UNION 涓€璧蜂娇鐢ㄦ椂锛堝嵆 UNION ALL锛夛紝涓嶆秷闄ら噸澶嶈銆備袱绉嶆儏鍐典笅锛屾淳鐢熻〃鐨勬瘡涓€琛屼笉鏄潵鑷?TABLE1 灏辨槸鏉ヨ嚜 TABLE2銆?/span>
B锛?EXCEPT 杩愮畻绗?/span>
EXCEPT 杩愮畻绗﹂€氳繃鍖呮嫭鎵€鏈夊湪 TABLE1 涓絾涓嶅湪 TABLE2 涓殑琛屽苟娑堥櫎鎵€鏈夐噸澶嶈鑰屾淳鐢熷嚭涓€涓粨鏋滆〃銆傚綋 ALL 闅?EXCEPT 涓€璧蜂娇鐢ㄦ椂 (EXCEPT ALL)锛屼笉娑堥櫎閲嶅琛屻€?/span>
C锛?INTERSECT 杩愮畻绗?/span>
INTERSECT 杩愮畻绗﹂€氳繃鍙寘鎷?TABLE1 鍜?TABLE2 涓兘鏈夌殑琛屽苟娑堥櫎鎵€鏈夐噸澶嶈鑰屾淳鐢熷嚭涓€涓粨鏋滆〃銆傚綋 ALL 闅?INTERSECT 涓€璧蜂娇鐢ㄦ椂 (INTERSECT ALL)锛屼笉娑堥櫎閲嶅琛屻€?/span>
娉細浣跨敤杩愮畻璇嶇殑鍑犱釜鏌ヨ缁撴灉琛屽繀椤绘槸涓€鑷寸殑銆?/span>
12銆佽鏄庯細浣跨敤澶栬繛鎺?/span>
A銆乴eft 锛坥uter锛?join锛?/span>
宸﹀杩炴帴锛堝乏杩炴帴锛夛細缁撴灉闆嗗嚑鍖呮嫭杩炴帴琛ㄧ殑鍖归厤琛岋紝涔熷寘鎷乏杩炴帴琛ㄧ殑鎵€鏈夎銆?/span>
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B锛歳ight 锛坥uter锛?join:
鍙冲杩炴帴(鍙宠繛鎺?锛氱粨鏋滈泦鏃㈠寘鎷繛鎺ヨ〃鐨勫尮閰嶈繛鎺ヨ锛屼篃鍖呮嫭鍙宠繛鎺ヨ〃鐨勬墍鏈夎銆?/span>
C锛歠ull/cross 锛坥uter锛?join锛?/span>
鍏ㄥ杩炴帴锛氫笉浠呭寘鎷鍙疯繛鎺ヨ〃鐨勫尮閰嶈锛岃繕鍖呮嫭涓や釜杩炴帴琛ㄤ腑鐨勬墍鏈夎褰曘€?/span>
12銆佸垎缁?Group by:
涓€寮犺〃锛屼竴鏃﹀垎缁勫畬鎴愬悗锛屾煡璇㈠悗鍙兘寰楀埌缁勭浉鍏崇殑淇℃伅銆?/span>
缁勭浉鍏崇殑淇℃伅锛氾紙缁熻淇℃伅锛?count,sum,max,min,avg 鍒嗙粍鐨勬爣鍑?
鍦⊿QLServer涓垎缁勬椂锛氫笉鑳戒互text,ntext,image绫诲瀷鐨勫瓧娈典綔涓哄垎缁勪緷鎹?/span>
鍦╯electe缁熻鍑芥暟涓殑瀛楁锛屼笉鑳藉拰鏅€氱殑瀛楁鏀惧湪涓€璧凤紱
13銆佸鏁版嵁搴撹繘琛屾搷浣滐細
鍒嗙鏁版嵁搴擄細 sp_detach_db; 闄勫姞鏁版嵁搴擄細sp_attach_db 鍚庢帴琛ㄦ槑锛岄檮鍔犻渶瑕佸畬鏁寸殑璺緞鍚?/span>
14.濡備綍淇敼鏁版嵁搴撶殑鍚嶇О:
sp_renamedb 鈥榦ld_name鈥? 鈥榥ew_name鈥?/span>
浜屻€佹彁鍗?/span>
1銆佽鏄庯細澶嶅埗琛?鍙鍒剁粨鏋?婧愯〃鍚嶏細a 鏂拌〃鍚嶏細b) (Access鍙敤)
娉曚竴锛歴elect * into b from a where 1<>1锛堜粎鐢ㄤ簬SQlServer锛?/span>
娉曚簩锛歴elect top 0 * into b from a
2銆佽鏄庯細鎷疯礉琛?鎷疯礉鏁版嵁,婧愯〃鍚嶏細a 鐩爣琛ㄥ悕锛歜) (Access鍙敤)
insert into b(a, b, c) select d,e,f from b;
3銆佽鏄庯細璺ㄦ暟鎹簱涔嬮棿琛ㄧ殑鎷疯礉(鍏蜂綋鏁版嵁浣跨敤缁濆璺緞) (Access鍙敤)
insert into b(a, b, c) select d,e,f from b in ‘鍏蜂綋鏁版嵁搴?rsquo; where 鏉′欢
渚嬪瓙锛?.from b in 鈥?&Server.MapPath(".")&"data.mdb" &"鈥?where..
4銆佽鏄庯細瀛愭煡璇?琛ㄥ悕1锛歛 琛ㄥ悕2锛歜)
select a,b,c from a where a IN (select d from b ) 鎴栬€? select a,b,c from a where a IN (1,2,3)
5銆佽鏄庯細鏄剧ず鏂囩珷銆佹彁浜や汉鍜屾渶鍚庡洖澶嶆椂闂?/span>
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
6銆佽鏄庯細澶栬繛鎺ユ煡璇?琛ㄥ悕1锛歛 琛ㄥ悕2锛歜)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
7銆佽鏄庯細鍦ㄧ嚎瑙嗗浘鏌ヨ(琛ㄥ悕1锛歛 )
select * from (SELECT a,b,c FROM a) T where t.a > 1;
8銆佽鏄庯細between鐨勭敤娉?between闄愬埗鏌ヨ鏁版嵁鑼冨洿鏃跺寘鎷簡杈圭晫鍊?not between涓嶅寘鎷?/span>
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 鏁板€? and 鏁板€?
9銆佽鏄庯細in 鐨勪娇鐢ㄦ柟娉?/span>
select * from table1 where a [not] in (‘鍊?’,’鍊?’,’鍊?’,’鍊?’)
10銆佽鏄庯細涓ゅ紶鍏宠仈琛紝鍒犻櫎涓昏〃涓凡缁忓湪鍓〃涓病鏈夌殑淇℃伅
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
11銆佽鏄庯細鍥涜〃鑱旀煡闂锛?/span>
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....
12銆佽鏄庯細鏃ョ▼瀹夋帓鎻愬墠浜斿垎閽熸彁閱?/span>
SQL: select * from 鏃ョ▼瀹夋帓 where datediff(鈥榤inute鈥?f寮€濮嬫椂闂?getdate())>5
13銆佽鏄庯細涓€鏉ql 璇彞鎼炲畾鏁版嵁搴撳垎椤?/span>
select top 10 b.* from (select top 20 涓婚敭瀛楁,鎺掑簭瀛楁 from 琛ㄥ悕 order by 鎺掑簭瀛楁 desc) a,琛ㄥ悕 b where b.涓婚敭瀛楁 = a.涓婚敭瀛楁 order by a.鎺掑簭瀛楁
鍏蜂綋瀹炵幇锛?/span>
鍏充簬鏁版嵁搴撳垎椤碉細
declare @start int,@end int
@sql nvarchar(600)
set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’
exec sp_executesql @sql
娉ㄦ剰锛氬湪top鍚庝笉鑳界洿鎺ヨ窡涓€涓彉閲忥紝鎵€浠ュ湪瀹為檯搴旂敤涓彧鏈夎繖鏍风殑杩涜鐗规畩鐨勫鐞嗐€俁id涓轰竴涓爣璇嗗垪锛屽鏋渢op鍚庤繕鏈夊叿浣撶殑瀛楁锛岃繖鏍峰仛鏄潪甯告湁濂藉鐨勩€傚洜涓鸿繖鏍峰彲浠ラ伩鍏?top鐨勫瓧娈靛鏋滄槸閫昏緫绱㈠紩鐨勶紝鏌ヨ鐨勭粨鏋滃悗瀹為檯琛ㄤ腑鐨勪笉涓€鑷达紙閫昏緫绱㈠紩涓殑鏁版嵁鏈夊彲鑳藉拰鏁版嵁琛ㄤ腑鐨勪笉涓€鑷达紝鑰屾煡璇㈡椂濡傛灉澶勫湪绱㈠紩鍒欓鍏堟煡璇㈢储寮曪級
14銆佽鏄庯細鍓?0鏉¤褰?/span>
select top 10 * form table1 where 鑼冨洿
15銆佽鏄庯細閫夋嫨鍦ㄦ瘡涓€缁刡鍊肩浉鍚岀殑鏁版嵁涓搴旂殑a鏈€澶х殑璁板綍鐨勬墍鏈変俊鎭?绫讳技杩欐牱鐨勭敤娉曞彲浠ョ敤浜庤鍧涙瘡鏈堟帓琛屾,姣忔湀鐑攢浜у搧鍒嗘瀽,鎸夌鐩垚缁╂帓鍚?绛夌瓑.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
16銆佽鏄庯細鍖呮嫭鎵€鏈夊湪 TableA 涓絾涓嶅湪 TableB鍜孴ableC 涓殑琛屽苟娑堥櫎鎵€鏈夐噸澶嶈鑰屾淳鐢熷嚭涓€涓粨鏋滆〃
(select a from tableA ) except (select a from tableB) except (select a from tableC)
17銆佽鏄庯細闅忔満鍙栧嚭10鏉℃暟鎹?/span>
select top 10 * from tablename order by newid()
18銆佽鏄庯細闅忔満閫夋嫨璁板綍
select newid()
19銆佽鏄庯細鍒犻櫎閲嶅璁板綍
1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
2),select distinct * into temp from tablename
delete from tablename
insert into tablename select * from temp
璇勪环锛?杩欑鎿嶄綔鐗佃繛澶ч噺鐨勬暟鎹殑绉诲姩锛岃繖绉嶅仛娉曚笉閫傚悎澶у閲忎絾鏁版嵁鎿嶄綔
3),渚嬪锛氬湪涓€涓閮ㄨ〃涓鍏ユ暟鎹紝鐢变簬鏌愪簺鍘熷洜绗竴娆″彧瀵煎叆浜嗕竴閮ㄥ垎锛屼絾寰堥毦鍒ゆ柇鍏蜂綋浣嶇疆锛岃繖鏍峰彧鏈夊湪涓嬩竴娆″叏閮ㄥ鍏ワ紝杩欐牱涔熷氨浜х敓濂藉閲嶅鐨勫瓧娈碉紝鎬庢牱鍒犻櫎閲嶅瀛楁
alter table tablename
--娣诲姞涓€涓嚜澧炲垪
add column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b) from tablename group by column1,column2,...)
alter table tablename drop column column_b
20銆佽鏄庯細鍒楀嚭鏁版嵁搴撻噷鎵€鏈夌殑琛ㄥ悕
select name from sysobjects where type=鈥楿鈥?// U浠h〃鐢ㄦ埛
21銆佽鏄庯細鍒楀嚭琛ㄩ噷鐨勬墍鏈夌殑鍒楀悕
select name from syscolumns where id=object_id(鈥楾ableName鈥?
22銆佽鏄庯細鍒楃ずtype銆乿ender銆乸cs瀛楁锛屼互type瀛楁鎺掑垪锛宑ase鍙互鏂逛究鍦板疄鐜板閲嶉€夋嫨锛岀被浼約elect 涓殑case銆?/span>
select type,sum(case vender when 鈥楢鈥?then pcs else 0 end),sum(case vender when 鈥楥鈥?then pcs else 0 end),sum(case vender when 鈥楤鈥?then pcs else 0 end) FROM tablename group by type
鏄剧ず缁撴灉锛?/span>
type vender pcs
鐢佃剳 A 1
鐢佃剳 A 1
鍏夌洏 B 2
鍏夌洏 A 2
鎵嬫満 B 3
鎵嬫満 C 3
23銆佽鏄庯細鍒濆鍖栬〃table1
TRUNCATE TABLE table1
24銆佽鏄庯細閫夋嫨浠?0鍒?5鐨勮褰?/span>
select top 5 * from (select top 15 * from table order by id asc) table_鍒悕 order by id desc
涓夈€佹妧宸?/span>
1銆?=1锛?=2鐨勪娇鐢紝鍦⊿QL璇彞缁勫悎鏃剁敤鐨勮緝澶?/span>
“where 1=1” 鏄〃绀洪€夋嫨鍏ㄩ儴 “where 1=2”鍏ㄩ儴涓嶉€夛紝
濡傦細
if @strWhere !=鈥樷€?/span>
begin
set @strSQL = 鈥榮elect count(*) as Total from [鈥?+ @tblName + 鈥榏 where 鈥?+ @strWhere
end
else
begin
set @strSQL = 鈥榮elect count(*) as Total from [鈥?+ @tblName + 鈥榏鈥?/span>
end
鎴戜滑鍙互鐩存帴鍐欐垚
閿欒锛佹湭鎵惧埌鐩綍椤广€?/span>
set @strSQL = 鈥榮elect count(*) as Total from [鈥?+ @tblName + 鈥榏 where 1=1 瀹夊畾 鈥? @strWhere 2銆佹敹缂╂暟鎹簱
--閲嶅缓绱㈠紩
DBCC REINDEX
DBCC INDEXDEFRAG
--鏀剁缉鏁版嵁鍜屾棩蹇?/span>
DBCC SHRINKDB
DBCC SHRINKFILE
3銆佸帇缂╂暟鎹簱
dbcc shrinkdatabase(dbname)
4銆佽浆绉绘暟鎹簱缁欐柊鐢ㄦ埛浠ュ凡瀛樺湪鐢ㄦ埛鏉冮檺
exec sp_change_users_login 鈥榰pdate_one鈥?鈥榥ewname鈥?鈥榦ldname鈥?/span>
go
5銆佹鏌ュ浠介泦
RESTORE VERIFYONLY from disk=鈥楨:dvbbs.bak鈥?/span>
6銆佷慨澶嶆暟鎹簱
ALTER DATABASE [dvbbs] SET SINGLE_USER
GO
DBCC CHECKDB(鈥榙vbbs鈥?repair_allow_data_loss) WITH TABLOCK
GO
ALTER DATABASE [dvbbs] SET MULTI_USER
GO
7銆佹棩蹇楁竻闄?/span>
SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
@MaxMinutes INT,
@NewSize INT
USE tablename -- 瑕佹搷浣滅殑鏁版嵁搴撳悕
SELECT @LogicalFileName = 鈥榯ablename_log鈥? -- 鏃ュ織鏂囦欢鍚?/span>
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 1 -- 浣犳兂璁惧畾鐨勬棩蹇楁枃浠剁殑澶у皬(M)
Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size
FROM sysfiles
WHERE name = @LogicalFileName
SELECT 鈥極riginal Size of 鈥?+ db_name() + 鈥?LOG is 鈥?+
CONVERT(VARCHAR(30),@OriginalSize) + 鈥?8K pages or 鈥?+
CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 鈥楳B鈥?/span>
FROM sysfiles
WHERE name = @LogicalFileName
CREATE TABLE DummyTrans
(DummyColumn char (8000) not null)
DECLARE @Counter INT,
@StartTime DATETIME,
@TruncLog VARCHAR(255)
SELECT @StartTime = GETDATE(),
@TruncLog = 鈥楤ACKUP LOG 鈥?+ db_name() + 鈥?WITH TRUNCATE_ONLY鈥?/span>
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)
AND (@OriginalSize * 8 /1024) > @NewSize
BEGIN -- Outer loop.
SELECT @Counter = 0
WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
BEGIN -- update
INSERT DummyTrans VALUES (鈥楩ill Log鈥? DELETE DummyTrans
SELECT @Counter = @Counter + 1
END
EXEC (@TruncLog)
END
SELECT 鈥楩inal Size of 鈥?+ db_name() + 鈥?LOG is 鈥?+
CONVERT(VARCHAR(30),size) + 鈥?8K pages or 鈥?+
CONVERT(VARCHAR(30),(size*8/1024)) + 鈥楳B鈥?/span>
FROM sysfiles
WHERE name = @LogicalFileName
DROP TABLE DummyTrans
SET NOCOUNT OFF
8銆佽鏄庯細鏇存敼鏌愪釜琛?/span>
exec sp_changeobjectowner 鈥榯ablename鈥?鈥榙bo鈥?/span>
9銆佸瓨鍌ㄦ洿鏀瑰叏閮ㄨ〃
CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
AS
DECLARE @Name as NVARCHAR(128)
DECLARE @Owner as NVARCHAR(128)
DECLARE @OwnerName as NVARCHAR(128)
DECLARE curObject CURSOR FOR
select 鈥楴ame鈥?nbsp; = name,
鈥極wner鈥?nbsp; = user_name(uid)
from sysobjects
where user_name(uid)=@OldOwner
order by name
OPEN curObject
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN
if @Owner=@OldOwner
begin
set @OwnerName = @OldOwner + 鈥?鈥?+ rtrim(@Name)
exec sp_changeobjectowner @OwnerName, @NewOwner
end
-- select @name,@NewOwner,@OldOwner
FETCH NEXT FROM curObject INTO @Name, @Owner
END
close curObject
deallocate curObject
GO
10銆丼QL SERVER涓洿鎺ュ惊鐜啓鍏ユ暟鎹?/span>
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end
妗堜緥锛?/span>
鏈夊涓嬭〃锛岃姹傚氨瑁变腑鎵€鏈夋矑鏈夊強鏍肩殑鎴愮妇锛屽湪姣忔澧為暦0.1鐨勫熀绀庝笂锛屼娇浠栧€戝墰濂藉強鏍?
Name score
Zhangshan 80
Lishi 59
Wangwu 50
Songquan 69
while((select min(score) from tb_table)<60)
begin
update tb_table set score =score*1.01
where score<60
if (select min(score) from tb_table)>60
break
else
continue
end
鏁版嵁寮€鍙?缁忓吀
1.鎸夊姘忕瑪鐢绘帓搴?
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //浠庡皯鍒板
2.鏁版嵁搴撳姞瀵?
select encrypt(鈥樺師濮嬪瘑鐮佲€?
select pwdencrypt(鈥樺師濮嬪瘑鐮佲€?
select pwdcompare(鈥樺師濮嬪瘑鐮佲€?鈥樺姞瀵嗗悗瀵嗙爜鈥? = 1--鐩稿悓锛涘惁鍒欎笉鐩稿悓 encrypt(鈥樺師濮嬪瘑鐮佲€?
select pwdencrypt(鈥樺師濮嬪瘑鐮佲€?
select pwdcompare(鈥樺師濮嬪瘑鐮佲€?鈥樺姞瀵嗗悗瀵嗙爜鈥? = 1--鐩稿悓锛涘惁鍒欎笉鐩稿悓
3.鍙栧洖琛ㄤ腑瀛楁:
declare @list varchar(1000),
@sql nvarchar(1000)
select @list=@list+鈥?鈥?b.name from sysobjects a,syscolumns b where a.id=b.id and a.name=鈥樿〃A鈥?/span>
set @sql=鈥榮elect 鈥?right(@list,len(@list)-1)+鈥?from 琛ˋ鈥?/span>
exec (@sql)
4.鏌ョ湅纭洏鍒嗗尯:
EXEC master..xp_fixeddrives
5.姣旇緝A,B琛ㄦ槸鍚︾浉绛?
if (select checksum_agg(binary_checksum(*)) from A)
=
(select checksum_agg(binary_checksum(*)) from B)
print 鈥樼浉绛夆€?/span>
else
print 鈥樹笉鐩哥瓑鈥?/span>
6.鏉€鎺夋墍鏈夌殑浜嬩欢鎺㈠療鍣ㄨ繘绋?
DECLARE hcforeach CURSOR GLOBAL FOR SELECT 鈥榢ill 鈥?RTRIM(spid) FROM master.dbo.sysprocesses
WHERE program_name IN(鈥楽QL profiler鈥?N鈥楽QL 浜嬩欢鎺㈡煡鍣ㄢ€?
EXEC sp_msforeach_worker 鈥?鈥?/span>
7.璁板綍鎼滅储:
寮€澶村埌N鏉¤褰?/span>
Select Top N * From 琛?/span>
-------------------------------
N鍒癕鏉¤褰?瑕佹湁涓荤储寮旾D)
Select Top M-N * From 琛?Where ID in (Select Top M ID From 琛? Order by ID Desc
----------------------------------
N鍒扮粨灏捐褰?/span>
Select Top N * From 琛?Order by ID Desc
妗堜緥
渚嬪1锛氫竴寮犺〃鏈変竴涓囧鏉¤褰曪紝琛ㄧ殑绗竴涓瓧娈?RecID 鏄嚜澧為暱瀛楁锛?鍐欎竴涓猄QL璇彞锛屾壘鍑鸿〃鐨勭31鍒扮40涓褰曘€?/span>
select top 10 recid from A where recid not in(select top 30 recid from A)
鍒嗘瀽锛氬鏋滆繖鏍峰啓浼氫骇鐢熸煇浜涢棶棰橈紝濡傛灉recid鍦ㄨ〃涓瓨鍦ㄩ€昏緫绱㈠紩銆?/span>
select top 10 recid from A where……鏄粠绱㈠紩涓煡鎵撅紝鑰屽悗闈㈢殑select top 30 recid from A鍒欏湪鏁版嵁琛ㄤ腑鏌ユ壘锛岃繖鏍风敱浜庣储寮曚腑鐨勯『搴忔湁鍙兘鍜屾暟鎹〃涓殑涓嶄竴鑷达紝杩欐牱灏卞鑷存煡璇㈠埌鐨勪笉鏄湰鏉ョ殑娆插緱鍒扮殑鏁版嵁銆?/span>
瑙e喅鏂规
1锛?鐢╫rder by select top 30 recid from A order by ricid 濡傛灉璇ュ瓧娈典笉鏄嚜澧為暱锛屽氨浼氬嚭鐜伴棶棰?/span>
2锛?鍦ㄩ偅涓瓙鏌ヨ涓篃鍔犳潯浠讹細select top 30 recid from A where recid>-1
渚?锛氭煡璇㈣〃涓殑鏈€鍚庝互鏉¤褰曪紝骞朵笉鐭ラ亾杩欎釜琛ㄥ叡鏈夊灏戞暟鎹?浠ュ強琛ㄧ粨鏋勩€?/span>
set @s = 鈥榮elect top 1 * from T where pid not in (select top 鈥?+ str(@count-1) + 鈥?pid from T)鈥?/span>
print @s exec sp_executesql @s
9锛氳幏鍙栧綋鍓嶆暟鎹簱涓殑鎵€鏈夌敤鎴疯〃
select Name from sysobjects where xtype=鈥榰鈥?and status>=0
10锛氳幏鍙栨煇涓€涓〃鐨勬墍鏈夊瓧娈?/span>
select name from syscolumns where id=object_id(鈥樿〃鍚嶁€?
select name from syscolumns where id in (select id from sysobjects where type = 鈥榰鈥?and name = 鈥樿〃鍚嶁€?
涓ょ鏂瑰紡鐨勬晥鏋滅浉鍚?/span>
11锛氭煡鐪嬩笌鏌愪竴涓〃鐩稿叧鐨勮鍥俱€佸瓨鍌ㄨ繃绋嬨€佸嚱鏁?/span>
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like 鈥?琛ㄥ悕%鈥?/span>
12锛氭煡鐪嬪綋鍓嶆暟鎹簱涓墍鏈夊瓨鍌ㄨ繃绋?/span>
select name as 瀛樺偍杩囩▼鍚嶇О from sysobjects where xtype=鈥楶鈥?/span>
13锛氭煡璇㈢敤鎴峰垱寤虹殑鎵€鏈夋暟鎹簱
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name=鈥榮a鈥?
鎴栬€?/span>
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
14锛氭煡璇㈡煇涓€涓〃鐨勫瓧娈靛拰鏁版嵁绫诲瀷
select column_name,data_type from information_schema.columns
where table_name = 鈥樿〃鍚嶁€?/span>
15锛氫笉鍚屾湇鍔″櫒鏁版嵁搴撲箣闂寸殑鏁版嵁鎿嶄綔
--鍒涘缓閾炬帴鏈嶅姟鍣?/span>
exec sp_addlinkedserver 鈥業TSV 鈥? 鈥?鈥? 鈥楽QLOLEDB 鈥? 鈥樿繙绋嬫湇鍔″櫒鍚嶆垨ip鍦板潃 鈥?/span>
exec sp_addlinkedsrvlogin 鈥業TSV 鈥? 鈥榝alse 鈥?null, 鈥樼敤鎴峰悕 鈥? 鈥樺瘑鐮?鈥?/span>
--鏌ヨ绀轰緥
select * from ITSV.鏁版嵁搴撳悕.dbo.琛ㄥ悕
--瀵煎叆绀轰緥
select * into 琛?from ITSV.鏁版嵁搴撳悕.dbo.琛ㄥ悕
--浠ュ悗涓嶅啀浣跨敤鏃跺垹闄ら摼鎺ユ湇鍔″櫒
exec sp_dropserver 鈥業TSV 鈥? 鈥榙roplogins 鈥?/span>
--杩炴帴杩滅▼/灞€鍩熺綉鏁版嵁(openrowset/openquery/opendatasource)
--1銆乷penrowset
--鏌ヨ绀轰緥
select * from openrowset( 鈥楽QLOLEDB 鈥? 鈥榮ql鏈嶅姟鍣ㄥ悕 鈥? 鈥樼敤鎴峰悕 鈥? 鈥樺瘑鐮?鈥?鏁版嵁搴撳悕.dbo.琛ㄥ悕)
--鐢熸垚鏈湴琛?/span>
select * into 琛?from openrowset( 鈥楽QLOLEDB 鈥? 鈥榮ql鏈嶅姟鍣ㄥ悕 鈥? 鈥樼敤鎴峰悕 鈥? 鈥樺瘑鐮?鈥?鏁版嵁搴撳悕.dbo.琛ㄥ悕)
--鎶婃湰鍦拌〃瀵煎叆杩滅▼琛?/span>
insert openrowset( 鈥楽QLOLEDB 鈥? 鈥榮ql鏈嶅姟鍣ㄥ悕 鈥? 鈥樼敤鎴峰悕 鈥? 鈥樺瘑鐮?鈥?鏁版嵁搴撳悕.dbo.琛ㄥ悕)
select *from 鏈湴琛?/span>
--鏇存柊鏈湴琛?/span>
update b
set b.鍒桝=a.鍒桝
from openrowset( 鈥楽QLOLEDB 鈥? 鈥榮ql鏈嶅姟鍣ㄥ悕 鈥? 鈥樼敤鎴峰悕 鈥? 鈥樺瘑鐮?鈥?鏁版嵁搴撳悕.dbo.琛ㄥ悕)as a inner join 鏈湴琛?b
on a.column1=b.column1
--openquery鐢ㄦ硶闇€瑕佸垱寤轰竴涓繛鎺?/span>
--棣栧厛鍒涘缓涓€涓繛鎺ュ垱寤洪摼鎺ユ湇鍔″櫒
exec sp_addlinkedserver 鈥業TSV 鈥? 鈥?鈥? 鈥楽QLOLEDB 鈥? 鈥樿繙绋嬫湇鍔″櫒鍚嶆垨ip鍦板潃 鈥?/span>
--鏌ヨ
select *
FROM openquery(ITSV, 鈥楽ELECT * FROM 鏁版嵁搴?dbo.琛ㄥ悕 鈥?
--鎶婃湰鍦拌〃瀵煎叆杩滅▼琛?/span>
insert openquery(ITSV, 鈥楽ELECT * FROM 鏁版嵁搴?dbo.琛ㄥ悕 鈥?
select * from 鏈湴琛?/span>
--鏇存柊鏈湴琛?/span>
update b
set b.鍒桞=a.鍒桞
FROM openquery(ITSV, 鈥楽ELECT * FROM 鏁版嵁搴?dbo.琛ㄥ悕 鈥? as a
inner join 鏈湴琛?b on a.鍒桝=b.鍒桝
--3銆乷pendatasource/openrowset
SELECT *
FROM opendatasource( 鈥楽QLOLEDB 鈥? 鈥楧ata Source=ip/ServerName;User ID=鐧婚檰鍚?Password=瀵嗙爜 鈥?).test.dbo.roy_ta
--鎶婃湰鍦拌〃瀵煎叆杩滅▼琛?/span>
insert opendatasource( 鈥楽QLOLEDB 鈥? 鈥楧ata Source=ip/ServerName;User ID=鐧婚檰鍚?Password=瀵嗙爜 鈥?.鏁版嵁搴?dbo.琛ㄥ悕
select * from 鏈湴琛?/span>
SQL Server鍩烘湰鍑芥暟
SQL Server鍩烘湰鍑芥暟
1.瀛楃涓插嚱鏁?闀垮害涓庡垎鏋愮敤
1,datalength(Char_expr) 杩斿洖瀛楃涓插寘鍚瓧绗︽暟,浣嗕笉鍖呭惈鍚庨潰鐨勭┖鏍?/span>
2,substring(expression,start,length) 鍙栧瓙涓诧紝瀛楃涓茬殑涓嬫爣鏄粠“1”锛宻tart涓鸿捣濮嬩綅缃紝length涓哄瓧绗︿覆闀垮害锛屽疄闄呭簲鐢ㄤ腑浠en(expression)鍙栧緱鍏堕暱搴?/span>
3,right(char_expr,int_expr) 杩斿洖瀛楃涓插彸杈圭int_expr涓瓧绗︼紝杩樼敤left浜庝箣鐩稿弽
4,isnull( check_expression , replacement_value )濡傛灉check_expression鐐虹┖锛屽墖杩斿洖replacement_value鐨勫€硷紝涓嶇偤绌猴紝灏辫繑鍥瀋heck_expression瀛楃鎿嶄綔绫?/span>
5,Sp_addtype 鑷畾缇╂暩鎿氶鍨?/span>
渚嬪锛欵XEC sp_addtype birthday, datetime, 鈥楴ULL鈥?/span>
6,set nocount {on|off}
浣胯繑鍥炵殑缁撴灉涓笉鍖呭惈鏈夊叧鍙?Transact-SQL 璇彞褰卞搷鐨勮鏁扮殑淇℃伅銆傚鏋滃瓨鍌ㄨ繃绋嬩腑鍖呭惈鐨勪竴浜涜鍙ュ苟涓嶈繑鍥炶澶氬疄闄呯殑鏁版嵁锛屽垯璇ヨ缃敱浜庡ぇ閲忓噺灏戜簡缃戠粶娴侀噺锛屽洜姝ゅ彲鏄捐憲鎻愰珮鎬ц兘銆係ET NOCOUNT 璁剧疆鏄湪鎵ц鎴栬繍琛屾椂璁剧疆锛岃€屼笉鏄湪鍒嗘瀽鏃惰缃€?/span>
SET NOCOUNT 涓?ON 鏃讹紝涓嶈繑鍥炶鏁帮紙琛ㄧず鍙?Transact-SQL 璇彞褰卞搷鐨勮鏁帮級銆?/span>
SET NOCOUNT 涓?OFF 鏃讹紝杩斿洖璁℃暟
甯歌瘑
鍦⊿QL鏌ヨ涓細from鍚庢渶澶氬彲浠ヨ窡澶氬皯寮犺〃鎴栬鍥撅細256
鍦⊿QL璇彞涓嚭鐜?Order by,鏌ヨ鏃讹紝鍏堟帓搴忥紝鍚庡彇
鍦⊿QL涓紝涓€涓瓧娈电殑鏈€澶у閲忔槸8000锛岃€屽浜巒varchar(4000),鐢变簬nvarchar鏄疷nicode鐮併€?/span>
SQLServer2000鍚屾澶嶅埗鎶€鏈疄鐜版楠?/span>
涓€銆?棰勫宸ヤ綔
1.鍙戝竷鏈嶅姟鍣?璁㈤槄鏈嶅姟鍣ㄩ兘鍒涘缓涓€涓悓鍚嶇殑windows鐢ㄦ埛,骞惰缃浉鍚岀殑瀵嗙爜,鍋氫负鍙戝竷蹇収鏂囦欢澶圭殑鏈夋晥璁块棶鐢ㄦ埛
--绠$悊宸ュ叿
--璁$畻鏈虹鐞?/span>
--鐢ㄦ埛鍜岀粍
--鍙抽敭鐢ㄦ埛
--鏂板缓鐢ㄦ埛
--寤虹珛涓€涓毝灞炰簬administrator缁勭殑鐧婚檰windows鐨勭敤鎴凤紙SynUser锛?/span>
2.鍦ㄥ彂甯冩湇鍔″櫒涓?鏂板缓涓€涓叡浜洰褰?鍋氫负鍙戝竷鐨勫揩鐓ф枃浠剁殑瀛樻斁鐩綍,鎿嶄綔:
鎴戠殑鐢佃剳--D: 鏂板缓涓€涓洰褰?鍚嶄负: PUB
--鍙抽敭杩欎釜鏂板缓鐨勭洰褰?/span>
--灞炴€?-鍏变韩
--閫夋嫨"鍏变韩璇ユ枃浠跺す"
--閫氳繃"鏉冮檺"鎸夌航鏉ヨ缃叿浣撶殑鐢ㄦ埛鏉冮檺,淇濊瘉绗竴姝ヤ腑鍒涘缓鐨勭敤鎴?SynUser) 鍏锋湁瀵硅鏂囦欢澶圭殑鎵€鏈夋潈闄?/span>
--纭畾
3.璁剧疆SQL浠g悊(SQLSERVERAGENT)鏈嶅姟鐨勫惎鍔ㄧ敤鎴?鍙戝竷/璁㈤槄鏈嶅姟鍣ㄥ潎鍋氭璁剧疆)
寮€濮?-绋嬪簭--绠$悊宸ュ叿--鏈嶅姟
--鍙抽敭SQLSERVERAGENT
--灞炴€?-鐧婚檰--閫夋嫨"姝よ处鎴?
--杈撳叆鎴栬€呴€夋嫨绗竴姝ヤ腑鍒涘缓鐨剋indows鐧诲綍鐢ㄦ埛鍚嶏紙SynUser锛?/span>
--"瀵嗙爜"涓緭鍏ヨ鐢ㄦ埛鐨勫瘑鐮?/span>
4.璁剧疆SQL Server韬唤楠岃瘉妯″紡,瑙e喅杩炴帴鏃剁殑鏉冮檺闂(鍙戝竷/璁㈤槄鏈嶅姟鍣ㄥ潎鍋氭璁剧疆)
浼佷笟绠$悊鍣?/span>
--鍙抽敭SQL瀹炰緥--灞炴€?/span>
--瀹夊叏鎬?-韬唤楠岃瘉
--閫夋嫨"SQL Server 鍜?Windows"
--纭畾
5.鍦ㄥ彂甯冩湇鍔″櫒鍜岃闃呮湇鍔″櫒涓婁簰鐩告敞鍐?/span>
浼佷笟绠$悊鍣?/span>
--鍙抽敭SQL Server缁?/span>
--鏂板缓SQL Server娉ㄥ唽...
--涓嬩竴姝?-鍙敤鐨勬湇鍔″櫒涓?杈撳叆浣犺娉ㄥ唽鐨勮繙绋嬫湇鍔″櫒鍚?--娣诲姞
--涓嬩竴姝?-杩炴帴浣跨敤,閫夋嫨绗簩涓?SQL Server韬唤楠岃瘉"
--涓嬩竴姝?-杈撳叆鐢ㄦ埛鍚嶅拰瀵嗙爜锛圫ynUser锛?/span>
--涓嬩竴姝?-閫夋嫨SQL Server缁?涔熷彲浠ュ垱寤轰竴涓柊缁?/span>
--涓嬩竴姝?-瀹屾垚
6.瀵逛簬鍙兘鐢↖P,涓嶈兘鐢ㄨ绠楁満鍚嶇殑,涓哄叾娉ㄥ唽鏈嶅姟鍣ㄥ埆鍚嶏紙姝ゆ鍦ㄥ疄鏂戒腑娌$敤鍒帮級
(鍦ㄨ繛鎺ョ閰嶇疆,姣斿,鍦ㄨ闃呮湇鍔″櫒涓婇厤缃殑璇?鏈嶅姟鍣ㄥ悕绉颁腑杈撳叆鐨勬槸鍙戝竷鏈嶅姟鍣ㄧ殑IP)
寮€濮?-绋嬪簭--Microsoft SQL Server--瀹㈡埛绔綉缁滃疄鐢ㄥ伐鍏?/span>
--鍒悕--娣诲姞
--缃戠粶搴撻€夋嫨"tcp/ip"--鏈嶅姟鍣ㄥ埆鍚嶈緭鍏QL鏈嶅姟鍣ㄥ悕
--杩炴帴鍙傛暟--鏈嶅姟鍣ㄥ悕绉颁腑杈撳叆SQL鏈嶅姟鍣╥p鍦板潃
--濡傛灉浣犱慨鏀逛簡SQL鐨勭鍙?鍙栨秷閫夋嫨"鍔ㄦ€佸喅瀹氱鍙?,骞惰緭鍏ュ搴旂殑绔彛鍙?/span>
浜屻€?姝e紡閰嶇疆
1銆侀厤缃彂甯冩湇鍔″櫒
鎵撳紑浼佷笟绠$悊鍣紝鍦ㄥ彂甯冩湇鍔″櫒锛圔銆丆銆丏锛変笂鎵ц浠ヤ笅姝ラ:
(1) 浠嶽宸ュ叿]涓嬫媺鑿滃崟鐨刐澶嶅埗]瀛愯彍鍗曚腑閫夋嫨[閰嶇疆鍙戝竷銆佽闃呮湇鍔″櫒鍜屽垎鍙慮鍑虹幇閰嶇疆鍙戝竷鍜屽垎鍙戝悜瀵?/span>
(2) [涓嬩竴姝 閫夋嫨鍒嗗彂鏈嶅姟鍣?鍙互閫夋嫨鎶婂彂甯冩湇鍔″櫒鑷繁浣滀负鍒嗗彂鏈嶅姟鍣ㄦ垨鑰呭叾浠杝ql鐨勬湇鍔″櫒锛堥€夋嫨鑷繁锛?/span>
(3) [涓嬩竴姝 璁剧疆蹇収鏂囦欢澶?/span>
閲囩敤榛樿\servernamePub
(4) [涓嬩竴姝 鑷畾涔夐厤缃?/span>
鍙互閫夋嫨:鏄?璁╂垜璁剧疆鍒嗗彂鏁版嵁搴撳睘鎬у惎鐢ㄥ彂甯冩湇鍔″櫒鎴栬缃彂甯冭缃?/span>
鍚?浣跨敤涓嬪垪榛樿璁剧疆锛堟帹鑽愶級
(5) [涓嬩竴姝 璁剧疆鍒嗗彂鏁版嵁搴撳悕绉板拰浣嶇疆 閲囩敤榛樿鍊?/span>
(6) [涓嬩竴姝 鍚敤鍙戝竷鏈嶅姟鍣?閫夋嫨浣滀负鍙戝竷鐨勬湇鍔″櫒
(7) [涓嬩竴姝 閫夋嫨闇€瑕佸彂甯冪殑鏁版嵁搴撳拰鍙戝竷绫诲瀷
(8) [涓嬩竴姝 閫夋嫨娉ㄥ唽璁㈤槄鏈嶅姟鍣?/span>
(9) [涓嬩竴姝 瀹屾垚閰嶇疆
2銆佸垱寤哄嚭鐗堢墿
鍙戝竷鏈嶅姟鍣˙銆丆銆丏涓?/span>
(1)浠嶽宸ュ叿]鑿滃崟鐨刐澶嶅埗]瀛愯彍鍗曚腑閫夋嫨[鍒涘缓鍜岀鐞嗗彂甯僝鍛戒护
(2)閫夋嫨瑕佸垱寤哄嚭鐗堢墿鐨勬暟鎹簱锛岀劧鍚庡崟鍑籟鍒涘缓鍙戝竷]
(3)鍦╗鍒涘缓鍙戝竷鍚戝]鐨勬彁绀哄璇濇涓崟鍑籟涓嬩竴姝绯荤粺灏变細寮瑰嚭涓€涓璇濇銆傚璇濇涓婄殑鍐呭鏄鍒剁殑涓変釜绫诲瀷銆傛垜浠幇鍦ㄩ€夌涓€涓篃灏辨槸榛樿鐨勫揩鐓у彂甯?鍏朵粬涓や釜澶у鍙互鍘荤湅鐪嬪府鍔?
(4)鍗曞嚮[涓嬩竴姝绯荤粺瑕佹眰鎸囧畾鍙互璁㈤槄璇ュ彂甯冪殑鏁版嵁搴撴湇鍔″櫒绫诲瀷,
SQLSERVER鍏佽鍦ㄤ笉鍚岀殑鏁版嵁搴撳 orACLE鎴朅CCESS涔嬮棿杩涜鏁版嵁澶嶅埗銆?/span>
浣嗘槸鍦ㄨ繖閲屾垜浠€夋嫨杩愯"SQL SERVER 2000"鐨勬暟鎹簱鏈嶅姟鍣?/span>
(5)鍗曞嚮[涓嬩竴姝绯荤粺灏卞脊鍑轰竴涓畾涔夋枃绔犵殑瀵硅瘽妗嗕篃灏辨槸閫夋嫨瑕佸嚭鐗堢殑琛?/span>
娉ㄦ剰: 濡傛灉鍓嶉潰閫夋嫨浜嗕簨鍔″彂甯?鍒欏啀杩欎竴姝ヤ腑鍙兘閫夋嫨甯︽湁涓婚敭鐨勮〃
(6)閫夋嫨鍙戝竷鍚嶇О鍜屾弿杩?/span>
(7)鑷畾涔夊彂甯冨睘鎬?鍚戝鎻愪緵鐨勯€夋嫨:
鏄?鎴戝皢鑷畾涔夋暟鎹瓫閫?鍚敤鍖垮悕璁㈤槄鍜屾垨鍏朵粬鑷畾涔夊睘鎬?/span>
鍚?鏍规嵁鎸囧畾鏂瑰紡鍒涘缓鍙戝竷 锛堝缓璁噰鐢ㄨ嚜瀹氫箟鐨勬柟寮忥級
(8)[涓嬩竴姝 閫夋嫨绛涢€夊彂甯冪殑鏂瑰紡
(9)[涓嬩竴姝 鍙互閫夋嫨鏄惁鍏佽鍖垮悕璁㈤槄
1)濡傛灉閫夋嫨缃插悕璁㈤槄,鍒欓渶瑕佸湪鍙戝竷鏈嶅姟鍣ㄤ笂娣诲姞璁㈤槄鏈嶅姟鍣?/span>
鏂规硶: [宸ュ叿]->[澶嶅埗]->[閰嶇疆鍙戝竷銆佽闃呮湇鍔″櫒鍜屽垎鍙戠殑灞炴€->[璁㈤槄鏈嶅姟鍣╙ 涓坊鍔?/span>
鍚﹀垯鍦ㄨ闃呮湇鍔″櫒涓婅姹傝闃呮椂浼氬嚭鐜扮殑鎻愮ず:鏀瑰彂甯冧笉鍏佽鍖垮悕璁㈤槄
濡傛灉浠嶇劧闇€瑕佸尶鍚嶈闃呭垯鐢ㄤ互涓嬭В鍐冲姙娉?/span>
[浼佷笟绠$悊鍣╙->[澶嶅埗]->[鍙戝竷鍐呭]->[灞炴€->[璁㈤槄閫夐」] 閫夋嫨鍏佽鍖垮悕璇锋眰璁㈤槄
2)濡傛灉閫夋嫨鍖垮悕璁㈤槄,鍒欓厤缃闃呮湇鍔″櫒鏃朵笉浼氬嚭鐜颁互涓婃彁绀?/span>
(10)[涓嬩竴姝 璁剧疆蹇収 浠g悊绋嬪簭璋冨害
(11)[涓嬩竴姝 瀹屾垚閰嶇疆
褰撳畬鎴愬嚭鐗堢墿鐨勫垱寤哄悗鍒涘缓鍑虹増鐗╃殑鏁版嵁搴撲篃灏卞彉鎴愪簡涓€涓叡浜暟鎹簱
鏈夋暟鎹?/span>
srv1.搴撳悕..author鏈夊瓧娈?id,name,phone,
srv2.搴撳悕..author鏈夊瓧娈?id,name,telphone,adress
瑕佹眰锛?/span>
srv1.搴撳悕..author澧炲姞璁板綍鍒檚rv1.搴撳悕..author璁板綍澧炲姞
srv1.搴撳悕..author鐨刾hone瀛楁鏇存柊锛屽垯srv1.搴撳悕..author瀵瑰簲瀛楁telphone鏇存柊
--*/
--澶ц嚧鐨勫鐞嗘楠?/span>
--1.鍦?srv1 涓婂垱寤鸿繛鎺ユ湇鍔″櫒,浠ヤ究鍦?srv1 涓搷浣?srv2,瀹炵幇鍚屾
exec sp_addlinkedserver 鈥榮rv2鈥?鈥樷€?鈥楽QLOLEDB鈥?鈥榮rv2鐨剆ql瀹炰緥鍚嶆垨ip鈥?/span>
exec sp_addlinkedsrvlogin 鈥榮rv2鈥?鈥榝alse鈥?null,鈥樼敤鎴峰悕鈥?鈥樺瘑鐮佲€?/span>
go
--2.鍦?srv1 鍜?srv2 杩欎袱鍙扮數鑴戜腑,鍚姩 msdtc(鍒嗗竷寮忎簨鍔″鐞嗘湇鍔?,骞朵笖璁剧疆涓鸿嚜鍔ㄥ惎鍔?/span>
銆傛垜鐨勭數鑴?-鎺у埗闈㈡澘--绠$悊宸ュ叿--鏈嶅姟--鍙抽敭 Distributed Transaction Coordinator--灞炴€?-鍚姩--骞跺皢鍚姩绫诲瀷璁剧疆涓鸿嚜鍔ㄥ惎鍔?/span>
go
--鐒跺悗鍒涘缓涓€涓綔涓氬畾鏃惰皟鐢ㄤ笂闈㈢殑鍚屾澶勭悊瀛樺偍杩囩▼灏辫浜?/span>
浼佷笟绠$悊鍣?/span>
--绠$悊
--SQL Server浠g悊
--鍙抽敭浣滀笟
--鏂板缓浣滀笟
--"甯歌"椤逛腑杈撳叆浣滀笟鍚嶇О
--"姝ラ"椤?/span>
--鏂板缓
--"姝ラ鍚?涓緭鍏ユ楠ゅ悕
--"绫诲瀷"涓€夋嫨"Transact-SQL 鑴氭湰(TSQL)"
--"鏁版嵁搴?閫夋嫨鎵ц鍛戒护鐨勬暟鎹簱
--"鍛戒护"涓緭鍏ヨ鎵ц鐨勮鍙? exec p_process
--纭畾
--"璋冨害"椤?/span>
--鏂板缓璋冨害
--"鍚嶇О"涓緭鍏ヨ皟搴﹀悕绉?/span>
--"璋冨害绫诲瀷"涓€夋嫨浣犵殑浣滀笟鎵ц瀹夋帓
--濡傛灉閫夋嫨"鍙嶅鍑虹幇"
--鐐?鏇存敼"鏉ヨ缃綘鐨勬椂闂村畨鎺?/span>
鐒跺悗灏哠QL Agent鏈嶅姟鍚姩,骞惰缃负鑷姩鍚姩,鍚﹀垯浣犵殑浣滀笟涓嶄細琚墽琛?/span>
璁剧疆鏂规硶:
鎴戠殑鐢佃剳--鎺у埗闈㈡澘--绠$悊宸ュ叿--鏈嶅姟--鍙抽敭 SQLSERVERAGENT--灞炴€?-鍚姩绫诲瀷--閫夋嫨"鑷姩鍚姩"--纭畾.
--3.瀹炵幇鍚屾澶勭悊鐨勬柟娉?,瀹氭椂鍚屾
--鍦╯rv1涓垱寤哄涓嬬殑鍚屾澶勭悊瀛樺偍杩囩▼
create proc p_process
as
--鏇存柊淇敼杩囩殑鏁版嵁
update b set name=i.name,telphone=i.telphone
from srv2.搴撳悕.dbo.author b,author i
where b.id=i.id and
(b.name <> i.name or b.telphone <> i.telphone)
--鎻掑叆鏂板鐨勬暟鎹?/span>
insert srv2.搴撳悕.dbo.author(id,name,telphone)
select id,name,telphone from author i
where not exists(
select * from srv2.搴撳悕.dbo.author where id=i.id)
--鍒犻櫎宸茬粡鍒犻櫎鐨勬暟鎹?濡傛灉闇€瑕佺殑璇?
delete b
from srv2.搴撳悕.dbo.author b
where not exists(
select * from author where id=b.id)
go
以上是关于sql璇彞的主要内容,如果未能解决你的问题,请参考以下文章
linq to sql 鍜?entity framework 杈撳嚭sql璇彞
SQL涔婭NSERT INTO SELECT 鍜孲ELECT INTO FROM 璇彞
SQL-W3School-楂樼骇锛歋QL CREATE INDEX 璇彞
MySQL鐨凷QL璇彞 - 鏁版嵁鎿嶄綔璇彞锛?锛? DO 璇彞
涓€瀵逛竴鍏宠仈鏌ヨ鏃朵娇鐢╮elation杩炶疮鎿嶄綔鏌ヨ鍚庯紝璋冪敤getLastSql()鏂规硶杈撳嚭鐨剆ql璇彞