SQL璇彞鎬荤粨
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL璇彞鎬荤粨相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/source' title='source'>source
process 鎬庢牱 鍏变韩鏁版嵁 閾炬帴 mep 璁℃暟1 鍩虹 1銆佽鏄庯細鍒涘缓鏁版嵁搴? CREATE DATABASE database-name 2銆佽鏄庯細鍒犻櫎鏁版嵁搴? drop database dbname 3銆佽鏄庯細澶囦唤sql server --- 鍒涘缓 澶囦唤鏁版嵁鐨?device USE master EXEC sp_addumpdevice 鈥?/span>disk鈥?/span>, 鈥?/span>testBack鈥?/span>, 鈥?/span>c:mssql7backupMyNwind_1.dat鈥?/span> --- 寮€濮?澶囦唤 BACKUP DATABASE pubs TO testBack 4銆佽鏄庯細鍒涘缓鏂拌〃 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 鏍规嵁宸叉湁鐨勮〃鍒涘缓鏂拌〃锛? A锛?/span>create table tab_new like tab_old (浣跨敤鏃ц〃鍒涘缓鏂拌〃) B锛?/span>create 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绫诲瀷鐨勯暱搴︺€? 7銆佽鏄庯細娣诲姞涓婚敭锛? Alter table tabname add primary key(col) 璇存槑锛氬垹闄や富閿細 Alter table tabname drop primary key(col) 8銆佽鏄庯細鍒涘缓绱㈠紩锛? create [unique] index idxname on tabname(col….) 鍒犻櫎绱㈠紩锛?/span>drop index idxname 娉細绱㈠紩鏄笉鍙洿鏀圭殑锛屾兂鏇存敼蹇呴』鍒犻櫎閲嶆柊寤恒€? 9銆佽鏄庯細 鍒涘缓瑙嗗浘锛?/span>create view viewname as select statement 鍒犻櫎瑙嗗浘锛?/span>drop view viewname 10銆佽鏄庯細鍑犱釜绠€鍗曠殑鍩烘湰鐨剆ql璇彞 閫夋嫨锛?/span>select * from table1 where 鑼冨洿 鎻掑叆锛?/span>insert into table1(field1,field2) values(value1,value2) 鍒犻櫎锛?/span>delete from table1 where 鑼冨洿鏇存柊锛?span style="color: #0000ff;">update table1 set field1=value1 where 鑼冨洿 鏌ユ壘锛?/span>select * from table1 where field1 like ’%value1%’ ---like鐨勮娉曞緢绮惧锛屾煡璧勬枡! 鎺掑簭锛?span style="color: #0000ff;">select * from table1 order by field1,field2 [desc] 鎬绘暟锛?/span>select count as totalcount from table1 姹傚拰锛?/span>select sum(field1) as sumvalue from table1 骞冲潎锛?/span>select avg(field1) as avgvalue from table1 鏈€澶э細select max(field1) as maxvalue from table1 鏈€灏忥細select min(field1) as minvalue from table1 11銆佽鏄庯細鍑犱釜楂樼骇鏌ヨ杩愮畻璇? A锛?/span>UNION 杩愮畻绗? UNION 杩愮畻绗﹂€氳繃缁勫悎鍏朵粬涓や釜缁撴灉琛紙渚嬪 TABLE1 鍜?TABLE2锛夊苟娑堝幓琛ㄤ腑浠讳綍閲嶅琛岃€屾淳鐢熷嚭涓€涓粨鏋滆〃銆傚綋 ALL 闅?UNION 涓€璧蜂娇鐢ㄦ椂锛堝嵆 UNION ALL锛夛紝涓嶆秷闄ら噸澶嶈銆備袱绉嶆儏鍐典笅锛屾淳鐢熻〃鐨勬瘡涓€琛屼笉鏄潵鑷?TABLE1 灏辨槸鏉ヨ嚜 TABLE2銆? B锛?/span>EXCEPT 杩愮畻绗? EXCEPT杩愮畻绗﹂€氳繃鍖呮嫭鎵€鏈夊湪 TABLE1 涓絾涓嶅湪 TABLE2 涓殑琛屽苟娑堥櫎鎵€鏈夐噸澶嶈鑰屾淳鐢熷嚭涓€涓粨鏋滆〃銆傚綋 ALL 闅?EXCEPT 涓€璧蜂娇鐢ㄦ椂 (EXCEPT ALL)锛屼笉娑堥櫎閲嶅琛屻€? C锛?/span>INTERSECT 杩愮畻绗? INTERSECT杩愮畻绗﹂€氳繃鍙寘鎷?TABLE1 鍜?TABLE2 涓兘鏈夌殑琛屽苟娑堥櫎鎵€鏈夐噸澶嶈鑰屾淳鐢熷嚭涓€涓粨鏋滆〃銆傚綋 ALL闅?INTERSECT 涓€璧蜂娇鐢ㄦ椂 (INTERSECT ALL)锛屼笉娑堥櫎閲嶅琛屻€? 娉細浣跨敤杩愮畻璇嶇殑鍑犱釜鏌ヨ缁撴灉琛屽繀椤绘槸涓€鑷寸殑銆? 12銆佽鏄庯細浣跨敤澶栬繛鎺? A銆?/span>left 锛?span style="color: #808080;">outer锛?join锛?宸﹀杩炴帴锛堝乏杩炴帴锛夛細缁撴灉闆嗗嚑鍖呮嫭杩炴帴琛ㄧ殑鍖归厤琛岋紝涔熷寘鎷乏杩炴帴琛ㄧ殑鎵€鏈夎銆?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锛?/span>right 锛?span style="color: #808080;">outer锛?join: 鍙冲杩炴帴(鍙宠繛鎺?锛氱粨鏋滈泦鏃㈠寘鎷繛鎺ヨ〃鐨勫尮閰嶈繛鎺ヨ锛屼篃鍖呮嫭鍙宠繛鎺ヨ〃鐨勬墍鏈夎銆?C锛?/span>full/cross 锛?span style="color: #808080;">outer锛?join锛?鍏ㄥ杩炴帴锛氫笉浠呭寘鎷鍙疯繛鎺ヨ〃鐨勫尮閰嶈锛岃繕鍖呮嫭涓や釜杩炴帴琛ㄤ腑鐨勬墍鏈夎褰曘€?12銆佸垎缁?Group by: 涓€寮犺〃锛屼竴鏃﹀垎缁?瀹屾垚鍚庯紝鏌ヨ鍚庡彧鑳藉緱鍒扮粍鐩稿叧鐨勪俊鎭€? 缁勭浉鍏崇殑淇℃伅锛氾紙缁熻淇℃伅锛?count,sum,max,min,avg 鍒嗙粍鐨勬爣鍑? 鍦⊿QLServer涓垎缁勬椂锛氫笉鑳戒互text,ntext,image绫诲瀷鐨勫瓧娈典綔涓哄垎缁勪緷鎹? 鍦╯electe缁熻鍑芥暟涓殑瀛楁锛屼笉鑳藉拰鏅€氱殑瀛楁鏀惧湪涓€璧凤紱 13銆佸鏁版嵁搴撹繘琛屾搷浣滐細 鍒嗙鏁版嵁搴擄細sp_detach_db;闄勫姞鏁版嵁搴擄細sp_attach_db 鍚庢帴琛ㄦ槑锛岄檮鍔犻渶瑕佸畬鏁寸殑璺緞鍚? 14.濡備綍淇敼鏁版嵁搴撶殑鍚嶇О: sp_renamedb 鈥?/span>old_name鈥?/span>, 鈥?/span>new_name鈥?/span> 2 鎻愬崌 1銆佽鏄庯細澶嶅埗琛?鍙鍒剁粨鏋?婧愯〃鍚嶏細a 鏂拌〃鍚嶏細b) (Access鍙敤) 娉曚竴锛?/span>select * into b from a where 1<>1锛堜粎鐢ㄤ簬SQlServer锛?娉曚簩锛?/span>select 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 鈥?/span>"&Server.MapPath(".")&"data.mdb" &"鈥?/span> 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銆佽鏄庯細鏄剧ず鏂囩珷銆佹彁浜や汉鍜屾渶鍚庡洖澶嶆椂闂? 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涓嶅寘鎷? select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 鏁板€? and 鏁板€? 9銆佽鏄庯細in 鐨勪娇鐢ㄦ柟娉? select * from table1 where a [not] in (‘鍊?’,’鍊?’,’鍊?’,’鍊?’) 10銆佽鏄庯細涓ゅ紶鍏宠仈琛紝鍒犻櫎涓昏〃涓凡缁忓湪鍓〃涓病鏈夌殑淇℃伅 delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 11銆佽鏄庯細鍥涜〃鑱旀煡闂锛? 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銆佽鏄庯細鏃ョ▼瀹夋帓鎻愬墠浜斿垎閽熸彁閱? SQL: select * from 鏃ョ▼瀹夋帓 where datediff(鈥?/span>minute鈥?/span>,f寮€濮嬫椂闂?getdate())>5 13銆佽鏄庯細涓€鏉ql 璇彞鎼炲畾鏁版嵁搴撳垎椤祍elect top 10 b.* from (select top 20 涓婚敭瀛楁,鎺掑簭瀛楁 from 琛ㄥ悕 order by 鎺掑簭瀛楁 desc) a,琛ㄥ悕 b where b.涓婚敭瀛楁 = a.涓婚敭瀛楁 order by a.鎺掑簭瀛楁鍏蜂綋瀹炵幇锛氬叧浜庢暟鎹簱鍒嗛〉锛? 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鏉¤褰? 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鏉℃暟鎹? 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=鈥?/span>U鈥?/span> // U浠h〃鐢ㄦ埛 21銆佽鏄庯細鍒楀嚭琛ㄩ噷鐨勬墍鏈夌殑鍒楀悕 select name from syscolumns where id=object_id(鈥?/span>TableName鈥?/span>) 22銆佽鏄庯細鍒楃ずtype銆乿ender銆乸cs瀛楁锛屼互type瀛楁鎺掑垪锛宑ase鍙互鏂逛究鍦板疄鐜板閲嶉€夋嫨锛岀被浼約elect 涓殑case銆? select type,sum(case vender when 鈥?/span>A鈥?/span> then pcs else 0 end),sum(case vender when 鈥?/span>C鈥?/span> then pcs else 0 end),sum(case vender when 鈥?/span>B鈥?/span> then pcs else 0 end) FROM tablename group by type 鏄剧ず缁撴灉锛? type vender pcs 鐢佃剳 A 1 鐢佃剳 A 1 鍏夌洏 B 2 鍏夌洏 A 2 鎵嬫満 B 3 鎵嬫満 C 3 23銆佽鏄庯細鍒濆鍖栬〃table1 TRUNCATE TABLE table1 24銆佽鏄庯細閫夋嫨浠?0鍒?5鐨勮褰? select top 5 * from (select top 15 * from table order by id asc) table_鍒悕 order by id desc 3 鎶€宸? 1銆?span style="color: #800000; font-weight: bold;">1=1锛?span style="color: #800000; font-weight: bold;">1=2鐨勪娇鐢紝鍦⊿QL璇彞缁勫悎鏃剁敤鐨勮緝澶? “where 1=1” 鏄〃绀洪€夋嫨鍏ㄩ儴 “where 1=2”鍏ㄩ儴涓嶉€夛紝 濡傦細 if @strWhere !=鈥樷€?/span> begin set @strSQL = 鈥?/span>select count(*) as Total from [鈥?/span> + @tblName + 鈥?/span>] where 鈥?/span> + @strWhere end else begin set @strSQL = 鈥?/span>select count(*) as Total from [鈥?/span> + @tblName + 鈥?/span>]鈥?/span> end 鎴戜滑鍙互鐩存帴鍐欐垚 閿欒锛佹湭鎵惧埌鐩綍椤广€? set @strSQL = 鈥?/span>select count(*) as Total from [鈥?/span> + @tblName + 鈥?/span>] where 1=1 瀹夊畾 鈥?/span>+ @strWhere 2銆佹敹缂╂暟鎹簱 --閲嶅缓绱㈠紩 DBCC REINDEX DBCC INDEXDEFRAG --鏀剁缉鏁版嵁鍜屾棩蹇?/span> DBCC SHRINKDB DBCC SHRINKFILE 3銆佸帇缂╂暟鎹簱 dbcc shrinkdatabase(dbname) 4銆佽浆绉绘暟鎹簱缁欐柊鐢ㄦ埛浠ュ凡瀛樺湪鐢ㄦ埛鏉冮檺 exec sp_change_users_login 鈥?/span>update_one鈥?/span>,鈥?/span>newname鈥?/span>,鈥?/span>oldname鈥?/span> go 5銆佹鏌ュ浠介泦 RESTORE VERIFYONLY from disk=鈥?/span>E:dvbbs.bak鈥?/span> 6銆佷慨澶嶆暟鎹簱 ALTER DATABASE [dvbbs] SET SINGLE_USER GO DBCC CHECKDB(鈥?/span>dvbbs鈥?/span>,repair_allow_data_loss) WITH TABLOCK GO ALTER DATABASE [dvbbs] SET MULTI_USER GO 7銆佹棩蹇楁竻闄? SET NOCOUNT ON DECLARE @LogicalFileName sysname, @MaxMinutes INT, @NewSize INT USE tablename -- 瑕佹搷浣滅殑鏁版嵁搴撳悕 SELECT @LogicalFileName = 鈥?/span>tablename_log鈥?/span>, -- 鏃ュ織鏂囦欢鍚?/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 鈥?/span>Original Size of 鈥?/span> + db_name() + 鈥?/span> LOG is 鈥?/span> + CONVERT(VARCHAR(30),@OriginalSize) + 鈥?/span> 8K pages or 鈥?/span> + CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 鈥?/span>MB鈥?/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 = 鈥?/span>BACKUP LOG 鈥?/span> + db_name() + 鈥?/span> 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 (鈥?/span>Fill Log鈥?/span>) DELETE DummyTrans SELECT @Counter = @Counter + 1 END EXEC (@TruncLog) END SELECT 鈥?/span>Final Size of 鈥?/span> + db_name() + 鈥?/span> LOG is 鈥?/span> + CONVERT(VARCHAR(30),size) + 鈥?/span> 8K pages or 鈥?/span> + CONVERT(VARCHAR(30),(size*8/1024)) + 鈥?/span>MB鈥?/span> FROM sysfiles WHERE name = @LogicalFileName DROP TABLE DummyTrans SET NOCOUNT OFF 8銆佽鏄庯細鏇存敼鏌愪釜琛? exec sp_changeobjectowner 鈥?/span>tablename鈥?/span>,鈥?/span>dbo鈥?/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 鈥?/span>Name鈥?/span> = name, 鈥?/span>Owner鈥?/span> = 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 + 鈥?/span>.鈥?/span> + 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涓洿鎺ュ惊鐜啓鍏ユ暟鎹? declare @i int set @i=1 while @i<30 begin insert into test (userid) values(@i) set @i=@i+1 end 妗堜緥锛? 鏈夊涓嬭〃锛岃姹傚氨瑁变腑鎵€鏈夋矑鏈夊強鏍肩殑鎴愮妇锛屽湪姣忔澧為暦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 4 鏁版嵁寮€鍙?span style="color: #808080;">-缁忓吀 1.鎸夊姘忕瑪鐢绘帓搴? Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //浠庡皯鍒板 2.鏁版嵁搴撳姞瀵?select encrypt(鈥?/span>鍘熷瀵嗙爜鈥?/span>) select pwdencrypt(鈥?/span>鍘熷瀵嗙爜鈥?/span>) select pwdcompare(鈥?/span>鍘熷瀵嗙爜鈥?/span>,鈥?/span>鍔犲瘑鍚庡瘑鐮?/span>鈥?/span>) = 1--鐩稿悓锛涘惁鍒欎笉鐩稿悓 encrypt(鈥樺師濮嬪瘑鐮佲€? select pwdencrypt(鈥?/span>鍘熷瀵嗙爜鈥?/span>) select pwdcompare(鈥?/span>鍘熷瀵嗙爜鈥?/span>,鈥?/span>鍔犲瘑鍚庡瘑鐮?/span>鈥?/span>) = 1--鐩稿悓锛涘惁鍒欎笉鐩稿悓 3.鍙栧洖琛ㄤ腑瀛楁: declare @list varchar(1000), @sql nvarchar(1000) select @list=@list+鈥?/span>,鈥?/span>+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name=鈥?/span>琛ˋ鈥?/span> set @sql=鈥?/span>select 鈥?/span>+right(@list,len(@list)-1)+鈥?/span> 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>鐩哥瓑鈥?/span> else print 鈥?/span>涓嶇浉绛?/span>鈥?/span> 6.鏉€鎺夋墍鏈夌殑浜嬩欢鎺㈠療鍣ㄨ繘绋? DECLARE hcforeach CURSOR GLOBAL FOR SELECT 鈥?/span>kill 鈥?/span>+RTRIM(spid) FROM master.dbo.sysprocesses WHERE program_name IN(鈥?/span>SQL profiler鈥?/span>,N鈥?/span>SQL 浜嬩欢鎺㈡煡鍣?/span>鈥?/span>) EXEC sp_msforeach_worker 鈥?/span>?鈥?/span> 7.璁板綍鎼滅储: 寮€澶村埌N鏉¤褰昐elect Top N * From 琛?span style="color: #008080;">------------------------------- N鍒癕鏉¤褰?瑕佹湁涓荤储寮旾D) Select Top M-N * From 琛?Where ID in (Select Top M ID From 琛? Order by ID Desc ---------------------------------- N鍒扮粨灏捐褰昐elect Top N * From 琛?Order by ID Desc 妗堜緥渚嬪1锛氫竴寮犺〃鏈変竴涓囧鏉¤褰曪紝琛ㄧ殑绗竴涓瓧娈?RecID 鏄嚜澧為暱瀛楁锛?鍐欎竴涓猄QL璇彞锛?鎵惧嚭琛ㄧ殑绗?1鍒扮40涓褰曘€? select top 10 recid from A where recid not in(select top 30 recid from A) 鍒嗘瀽锛氬鏋滆繖鏍峰啓浼氫骇鐢熸煇浜涢棶棰橈紝濡傛灉recid鍦ㄨ〃涓瓨鍦ㄩ€昏緫绱㈠紩銆? select top 10 recid from A where……鏄粠绱㈠紩涓煡鎵撅紝鑰屽悗闈㈢殑select top 30 recid from A鍒欏湪鏁版嵁琛ㄤ腑鏌ユ壘锛岃繖鏍风敱浜庣储寮曚腑鐨勯『搴忔湁鍙兘鍜屾暟鎹〃涓殑涓嶄竴鑷达紝杩欐牱灏卞鑷存煡璇㈠埌鐨勪笉鏄湰鏉ョ殑娆插緱鍒扮殑鏁版嵁銆? 瑙e喅鏂规 1锛岀敤order by select top 30 recid from A order by ricid 濡傛灉璇ュ瓧娈典笉鏄嚜澧為暱锛屽氨浼氬嚭鐜伴棶棰? 2锛屽湪閭d釜瀛愭煡璇腑涔熷姞鏉′欢锛?span style="color: #0000ff;">select top 30 recid from A where recid>-1 渚?锛氭煡璇㈣〃涓殑鏈€鍚庝互鏉¤褰曪紝骞朵笉鐭ラ亾杩欎釜琛ㄥ叡鏈夊灏戞暟鎹?浠ュ強琛ㄧ粨鏋勩€? set @s = 鈥?/span>select top 1 * from T where pid not in (select top 鈥?/span> + str(@count-1) + 鈥?/span> pid from T)鈥?/span> print @s exec sp_executesql @s 9锛氳幏鍙栧綋鍓嶆暟鎹簱涓殑鎵€鏈夌敤鎴疯〃 select Name from sysobjects where xtype=鈥?/span>u鈥?/span> and status>=0 10锛氳幏鍙栨煇涓€涓〃鐨勬墍鏈夊瓧娈? select name from syscolumns where id=object_id(鈥?/span>琛ㄥ悕鈥?/span>) select name from syscolumns where id in (select id from sysobjects where type = 鈥?/span>u鈥?/span> and name = 鈥?/span>琛ㄥ悕鈥?/span>) 涓ょ鏂瑰紡鐨勬晥鏋滅浉鍚? 11锛氭煡鐪嬩笌鏌愪竴涓〃鐩稿叧鐨勮鍥俱€佸瓨鍌ㄨ繃绋嬨€佸嚱鏁? select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like 鈥?/span>%琛ㄥ悕%鈥?/span> 12锛氭煡鐪嬪綋鍓嶆暟鎹簱涓墍鏈夊瓨鍌ㄨ繃绋? select name as 瀛樺偍杩囩▼鍚嶇О from sysobjects where xtype=鈥?/span>P鈥?/span> 13锛氭煡璇㈢敤鎴峰垱寤虹殑鎵€鏈夋暟鎹簱 select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name=鈥?/span>sa鈥?/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>琛ㄥ悕鈥?/span> 15锛氫笉鍚屾湇鍔″櫒鏁版嵁搴撲箣闂寸殑鏁版嵁鎿嶄綔 --鍒涘缓閾炬帴鏈嶅姟鍣?/span> exec sp_addlinkedserver 鈥?/span>ITSV 鈥?/span>, 鈥?/span> 鈥?/span>, 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>杩滅▼鏈嶅姟鍣ㄥ悕鎴杋p鍦板潃 鈥?/span> exec sp_addlinkedsrvlogin 鈥?/span>ITSV 鈥?/span>, 鈥?/span>false 鈥?/span>,null, 鈥?/span>鐢ㄦ埛鍚?鈥?/span>, 鈥?/span>瀵嗙爜 鈥?/span> --鏌ヨ绀轰緥 select * from ITSV.鏁版嵁搴撳悕.dbo.琛ㄥ悕 --瀵煎叆绀轰緥 select * into 琛?from ITSV.鏁版嵁搴撳悕.dbo.琛ㄥ悕 --浠ュ悗涓嶅啀浣跨敤鏃跺垹闄ら摼鎺ユ湇鍔″櫒 exec sp_dropserver 鈥?/span>ITSV 鈥?/span>, 鈥?/span>droplogins 鈥?/span> --杩炴帴杩滅▼/灞€鍩熺綉鏁版嵁(openrowset/openquery/opendatasource) --1銆乷penrowset --鏌ヨ绀轰緥 select * from openrowset( 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>sql鏈嶅姟鍣ㄥ悕 鈥?/span>; 鈥?/span>鐢ㄦ埛鍚?鈥?/span>; 鈥?/span>瀵嗙爜 鈥?/span>,鏁版嵁搴撳悕.dbo.琛ㄥ悕) --鐢熸垚鏈湴琛?/span> select * into 琛?from openrowset( 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>sql鏈嶅姟鍣ㄥ悕 鈥?/span>; 鈥?/span>鐢ㄦ埛鍚?鈥?/span>; 鈥?/span>瀵嗙爜 鈥?/span>,鏁版嵁搴撳悕.dbo.琛ㄥ悕) --鎶婃湰鍦拌〃瀵煎叆杩滅▼琛?/span> insert openrowset( 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>sql鏈嶅姟鍣ㄥ悕 鈥?/span>; 鈥?/span>鐢ㄦ埛鍚?鈥?/span>; 鈥?/span>瀵嗙爜 鈥?/span>,鏁版嵁搴撳悕.dbo.琛ㄥ悕) select *from 鏈湴琛?--鏇存柊鏈湴琛?/span> update b set b.鍒桝=a.鍒桝 from openrowset( 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>sql鏈嶅姟鍣ㄥ悕 鈥?/span>; 鈥?/span>鐢ㄦ埛鍚?鈥?/span>; 鈥?/span>瀵嗙爜 鈥?/span>,鏁版嵁搴撳悕.dbo.琛ㄥ悕)as a inner join 鏈湴琛?b on a.column1=b.column1 --openquery鐢ㄦ硶闇€瑕佸垱寤轰竴涓繛鎺?/span> --棣栧厛鍒涘缓涓€涓繛鎺ュ垱寤洪摼鎺ユ湇鍔″櫒 exec sp_addlinkedserver 鈥?/span>ITSV 鈥?/span>, 鈥?/span> 鈥?/span>, 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>杩滅▼鏈嶅姟鍣ㄥ悕鎴杋p鍦板潃 鈥?/span> --鏌ヨ select * FROM openquery(ITSV, 鈥?/span>SELECT * FROM 鏁版嵁搴?dbo.琛ㄥ悕 鈥?/span>) --鎶婃湰鍦拌〃瀵煎叆杩滅▼琛?/span> insert openquery(ITSV, 鈥?/span>SELECT * FROM 鏁版嵁搴?dbo.琛ㄥ悕 鈥?/span>) select * from 鏈湴琛?--鏇存柊鏈湴琛?/span> update b set b.鍒桞=a.鍒桞 FROM openquery(ITSV, 鈥?/span>SELECT * FROM 鏁版嵁搴?dbo.琛ㄥ悕 鈥?/span>) as a inner join 鏈湴琛?b on a.鍒桝=b.鍒桝 --3銆乷pendatasource/openrowset SELECT * FROM opendatasource( 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>Data Source=ip/ServerName;User ID=鐧婚檰鍚?Password=瀵嗙爜 鈥?/span> ).test.dbo.roy_ta --鎶婃湰鍦拌〃瀵煎叆杩滅▼琛?/span> insert opendatasource( 鈥?/span>SQLOLEDB 鈥?/span>, 鈥?/span>Data Source=ip/ServerName;User ID=鐧婚檰鍚?Password=瀵嗙爜 鈥?/span>).鏁版嵁搴?dbo.琛ㄥ悕 select * from 鏈湴琛?5 SQL Server鍩烘湰鍑芥暟 SQL Server鍩烘湰鍑芥暟 1.瀛楃涓插嚱鏁?闀垮害涓庡垎鏋愮敤 1,datalength(Char_expr) 杩斿洖瀛楃涓插寘鍚瓧绗︽暟,浣嗕笉鍖呭惈鍚庨潰鐨勭┖鏍? 2,substring(expression,start,length) 鍙栧瓙涓诧紝瀛楃涓茬殑涓嬫爣鏄粠“1”锛宻tart涓鸿捣濮嬩綅缃紝length涓哄瓧绗︿覆闀垮害锛屽疄闄呭簲鐢ㄤ腑浠en(expression)鍙栧緱鍏堕暱搴? 3,right(char_expr,int_expr) 杩斿洖瀛楃涓插彸杈圭int_expr涓瓧绗︼紝杩樼敤left浜庝箣鐩稿弽 4,isnull( check_expression , replacement_value )濡傛灉check_expression鐐虹┖锛屽墖杩斿洖replacement_value鐨勫€硷紝涓嶇偤绌猴紝灏辫繑鍥瀋heck_expression瀛楃鎿嶄綔绫? 5,Sp_addtype鑷畾缇╂暩鎿氶鍨? 渚嬪锛?/span>EXEC sp_addtype birthday, datetime, 鈥?/span>NULL鈥?/span> 6,set nocount {on|off} 浣胯繑鍥炵殑缁撴灉涓笉鍖呭惈鏈夊叧鍙?Transact-SQL 璇彞褰卞搷鐨勮鏁扮殑淇℃伅銆傚鏋滃瓨鍌ㄨ繃绋嬩腑鍖呭惈鐨勪竴浜涜鍙ュ苟涓嶈繑鍥炶澶氬疄闄呯殑鏁版嵁锛屽垯璇ヨ缃敱浜庡ぇ閲忓噺灏戜簡缃戠粶娴侀噺锛屽洜姝ゅ彲鏄捐憲鎻愰珮鎬ц兘銆?span style="color: #0000ff;">SET NOCOUNT 璁剧疆鏄湪鎵ц鎴栬繍琛屾椂璁剧疆锛岃€屼笉鏄湪鍒嗘瀽鏃惰缃€? SET NOCOUNT 涓?ON 鏃讹紝涓嶈繑鍥炶鏁帮紙琛ㄧず鍙?Transact-SQL 璇彞褰卞搷鐨勮鏁帮級銆? SET NOCOUNT 涓?OFF 鏃讹紝杩斿洖璁℃暟 6 甯歌瘑 鍦⊿QL鏌ヨ涓細from鍚庢渶澶氬彲浠ヨ窡澶氬皯寮犺〃鎴栬鍥撅細256 鍦⊿QL璇彞涓嚭鐜?Order by,鏌ヨ鏃讹紝鍏堟帓搴忥紝鍚庡彇 鍦⊿QL涓紝涓€涓瓧娈电殑鏈€澶у閲忔槸8000锛岃€屽浜巒varchar(4000),鐢变簬nvarchar鏄疷nicode鐮併€? SQLServer2000鍚屾澶嶅埗鎶€鏈疄鐜版楠? 涓€銆?棰勫宸ヤ綔 1.鍙戝竷鏈嶅姟鍣?璁㈤槄鏈嶅姟鍣ㄩ兘鍒涘缓涓€涓悓鍚嶇殑windows鐢ㄦ埛,骞惰缃浉鍚岀殑瀵嗙爜,鍋氫负鍙戝竷蹇収鏂囦欢澶圭殑鏈夋晥璁块棶鐢ㄦ埛 --绠$悊宸ュ叿 --璁$畻鏈虹鐞?/span> --鐢ㄦ埛鍜岀粍 --鍙抽敭鐢ㄦ埛 --鏂板缓鐢ㄦ埛 --寤虹珛涓€涓毝灞炰簬administrator缁勭殑鐧婚檰windows鐨勭敤鎴凤紙SynUser锛?/span> 2.鍦ㄥ彂甯冩湇鍔″櫒涓?鏂板缓涓€涓叡浜洰褰?鍋氫负鍙戝竷鐨勫揩鐓ф枃浠剁殑瀛樻斁鐩綍,鎿嶄綔: 鎴戠殑鐢佃剳--D: 鏂板缓涓€涓洰褰?鍚嶄负: PUB --鍙抽敭杩欎釜鏂板缓鐨勭洰褰?/span> --灞炴€?-鍏变韩 --閫夋嫨"鍏变韩璇ユ枃浠跺す" --閫氳繃"鏉冮檺"鎸夌航鏉ヨ缃叿浣撶殑鐢ㄦ埛鏉冮檺,淇濊瘉绗竴姝ヤ腑鍒涘缓鐨勭敤鎴?SynUser) 鍏锋湁瀵硅鏂囦欢澶圭殑鎵€鏈夋潈闄?/span> --纭畾 3.璁剧疆SQL浠g悊(SQLSERVERAGENT)鏈嶅姟鐨勫惎鍔ㄧ敤鎴?鍙戝竷/璁㈤槄鏈嶅姟鍣ㄥ潎鍋氭璁剧疆) 寮€濮?/span>--绋嬪簭--绠$悊宸ュ叿--鏈嶅姟 --鍙抽敭SQLSERVERAGENT --灞炴€?-鐧婚檰--閫夋嫨"姝よ处鎴? --杈撳叆鎴栬€呴€夋嫨绗竴姝ヤ腑鍒涘缓鐨剋indows鐧诲綍鐢ㄦ埛鍚嶏紙SynUser锛?/span> --"瀵嗙爜"涓緭鍏ヨ鐢ㄦ埛鐨勫瘑鐮?/span> 4.璁剧疆SQL Server韬唤楠岃瘉妯″紡,瑙e喅杩炴帴鏃剁殑鏉冮檺闂(鍙戝竷/璁㈤槄鏈嶅姟鍣ㄥ潎鍋氭璁剧疆) 浼佷笟绠$悊鍣?--鍙抽敭SQL瀹炰緥--灞炴€?/span> --瀹夊叏鎬?-韬唤楠岃瘉 --閫夋嫨"SQL Server 鍜?Windows" --纭畾 5.鍦ㄥ彂甯冩湇鍔″櫒鍜岃闃呮湇鍔″櫒涓婁簰鐩告敞鍐? 浼佷笟绠$悊鍣?--鍙抽敭SQL Server缁?/span> --鏂板缓SQL Server娉ㄥ唽... --涓嬩竴姝?-鍙敤鐨勬湇鍔″櫒涓?杈撳叆浣犺娉ㄥ唽鐨勮繙绋嬫湇鍔″櫒鍚?--娣诲姞 --涓嬩竴姝?-杩炴帴浣跨敤,閫夋嫨绗簩涓?SQL Server韬唤楠岃瘉" --涓嬩竴姝?-杈撳叆鐢ㄦ埛鍚嶅拰瀵嗙爜锛圫ynUser锛?/span> --涓嬩竴姝?-閫夋嫨SQL Server缁?涔熷彲浠ュ垱寤轰竴涓柊缁?/span> --涓嬩竴姝?-瀹屾垚 6.瀵逛簬鍙兘鐢↖P,涓嶈兘鐢ㄨ绠楁満鍚嶇殑,涓哄叾娉ㄥ唽鏈嶅姟鍣ㄥ埆鍚嶏紙姝ゆ鍦ㄥ疄鏂戒腑娌$敤鍒帮級 (鍦ㄨ繛鎺ョ閰嶇疆,姣斿,鍦ㄨ闃呮湇鍔″櫒涓婇厤缃殑璇?鏈嶅姟鍣ㄥ悕绉颁腑杈撳叆鐨勬槸鍙戝竷鏈嶅姟鍣ㄧ殑IP) 寮€濮?/span>--绋嬪簭--Microsoft SQL Server--瀹㈡埛绔綉缁滃疄鐢ㄥ伐鍏?/span>