找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2037|回复: 0
打印 上一主题 下一主题

快速度查找文件和目录的SQL语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
查找文件的语句
8 h7 c  {$ U5 ]# ]- L1 ^% `' m0 p- c" R
CODE:* s! q/ @, ?) f  u  l& i. W0 s0 Y# ^
* i: \; u% t. U- F% V  q' r) g" O' M
drop table tmp;' K5 A5 X2 F; e( Y; |
create table tmp/ c* t7 r! y& N1 z$ q7 S8 N
(" [. M* G% J, G. j) {
[id] [int] IDENTITY (1,1) NOT NULL,( D( F' k6 K) [
[name] [nvarchar] (300) NOT NULL,
! d# Y- w( J2 o! R6 g" ]5 p[depth] [int] NOT NULL,+ d9 G4 W# @  E: `, o; w5 E8 P1 w: T
[isfile] [nvarchar] (50) NULL
' j; l8 J/ K8 ^8 |4 E  e4 O);8 I! [& \( U1 P( w+ ?
& [; `! c2 u; n4 u# h+ y
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)! e/ T9 _) _, u* W1 l
set @root='f:\usr\' -- Start root6 V* \+ h! o5 q: [
set @name='cmd.exe'   -- Find file
% L* x% C1 |/ ?4 f* }5 qinsert into tmp exec master..xp_dirtree @root,0,1--
+ X  P$ G9 g5 Q* ]set @id=(select top 1 id from tmp where isfile=1 and name=@name)
" R; A2 @* _/ t/ W! M. Zset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
% \- V3 _0 p( J% R' _: |while @depth<>1 ' l' I, @; X6 x4 \6 ]' z; ~2 g
begin
! |% i8 H8 b( K7 \set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) * y/ c' S0 H) @: M6 Q
set @depth=(select depth from tmp where id=@id)
' I+ r7 b# ~) j9 ]set @name=(select name from tmp where id=@id)+'\'+@name: X# V' X- ^6 Z& r
end4 Z- u$ L: }7 M4 }
update tmp set name=@root+@name where id=1
% _* m% i7 J; {! F- lselect name from tmp where id=1
8 G' ?- y& d- j3 q! p0 T  G2 r. c+ Y! A' x* T6 q
查找目录的语句
! e& J* U1 ~$ Y& H" V
: {) H( e& {8 X1 t- T. M) r# ~
8 ^4 U+ A; O* p( r( dCODE:
# o, f5 [  _9 b( |8 p; g6 f2 R( l  J9 ?; Q$ ?3 }0 g& R3 G* S5 ^3 d
' A& G# f* n2 L* D
drop table tmp;
/ Q  u5 y9 U; t+ F9 \# Fcreate table tmp0 a; A1 t. ^' ]) M) G( g
(
. P8 ~! G7 t: ?! u[id] [int] IDENTITY (1,1) NOT NULL,
8 j% J! D$ Y' N5 @$ x; ~4 D! W+ I3 x[name] [nvarchar] (300) NOT NULL,9 O3 J- P" I1 J1 @: M
[depth] [int] NOT NULL
- p; r3 `% N; X9 g+ T);- Z! C+ p" Q% `# o/ z' c1 k

0 o+ [$ ?" @  `1 Mdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)8 M: J& p: `4 J$ d% w
set @root='f:\usr\' -- Start root
6 P5 j5 Q9 R5 y7 f" nset @name='donggeer' -- directory to find
$ Z) v1 I2 L6 A0 Zinsert into tmp exec master..xp_dirtree @root,0,0
7 E% v3 {6 o5 H) B, C, Bset @id=(select top 1 id from tmp where name=@name)
2 Y+ `3 f) J& w. Cset @depth=(select top 1 depth from tmp where name=@name)
' E. L9 A% L( c( Z+ jwhile @depth<>1 2 m1 v6 b) i4 w0 i
begin ) d+ ^, z* N  I' L) ]) W6 N
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)# v" c$ a3 y: K% i
set @depth=(select depth from tmp where id=@id)
; P- Z2 q  j3 z$ @5 G; |set @name=(select name from tmp where id=@id)+'\'+@name 1 }9 x3 ]2 L' Y+ s. @
end update tmp set name=@root+@name where id=1
- Y, {( [+ i0 I  v, v1 ]! eselect name from tmp where id=1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表