sql璇彞

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql璇彞相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/format' title='format'>format   杩炴帴   灞€鍩熺綉   test   outer   ota   tsql   鐜板湪   flow   

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

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璇彞