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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
查找文件的语句" M& d6 `" ?  i* d# x9 G8 O

7 `& `& W/ g$ @/ J9 U6 Z" r8 VCODE:; C, d5 d, U* v* O) C* T

: F1 Q2 q# O. G% Mdrop table tmp;
; ]5 O2 o  U) Icreate table tmp
. w+ O* n9 y0 k4 g  {( P(7 |1 Q1 [; x4 z8 S
[id] [int] IDENTITY (1,1) NOT NULL,/ x7 V% f3 I7 D& t- N  P
[name] [nvarchar] (300) NOT NULL,
$ H/ y/ ?* Z3 ^2 a9 ], j[depth] [int] NOT NULL,- b9 ^' T' ^0 X/ X7 w
[isfile] [nvarchar] (50) NULL
& `- [4 _% _7 W);
2 Q% R0 l8 }! _$ [
- w; r7 l/ h# J! V7 Q# R8 Adeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)- k8 n1 y9 U+ Y+ a( ~' K5 `  }
set @root='f:\usr\' -- Start root: j; U; {  E7 G9 Y
set @name='cmd.exe'   -- Find file0 P4 Z, j1 y) [' n1 D
insert into tmp exec master..xp_dirtree @root,0,1--% `) \/ n$ u- s9 U( |  @
set @id=(select top 1 id from tmp where isfile=1 and name=@name) : f) m9 ~" L$ \
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)6 T3 e  ]6 ]% ~/ N) Y2 u
while @depth<>1
' ]" n  F# n0 zbegin 8 D% j) E) w0 n- H- x6 R6 V
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ( D0 \1 v2 c* {+ l* v, A5 i6 U
set @depth=(select depth from tmp where id=@id) , g/ X- i7 E- f
set @name=(select name from tmp where id=@id)+'\'+@name, s7 r/ N+ c' t+ S
end
# U6 A) g  H5 a5 Z' M% \; rupdate tmp set name=@root+@name where id=1# ^' T6 w) H# R! C% e/ `$ Z8 l% c" ?  i
select name from tmp where id=1
8 Q3 Q; U4 i( u" H3 d+ W" t. j1 M! p! J8 L2 z
查找目录的语句
1 x( Z5 |9 R/ U0 x& B! y! c. V2 W: g0 K; D- }9 w) D; l
- v, _; O0 [  F# y. b
CODE:
$ f0 o/ C  K0 d* m# b' e9 W6 i/ O) A$ I4 Q
: D3 ]/ w4 o+ I4 F. z
drop table tmp;
$ X' I: W* [% r, Z0 _, Icreate table tmp
% _, _! N' A8 v4 B! u: N) B1 u, H(- I6 J# J! m: y5 _- E& ]9 H
[id] [int] IDENTITY (1,1) NOT NULL,' K, y: I( M% M6 d8 c9 Q' j
[name] [nvarchar] (300) NOT NULL,; [' w* S1 D: m, g. v
[depth] [int] NOT NULL
7 _: j) T4 ~! a3 S- x);& P; A  D) t# l- B) O) t9 \0 y3 G

3 Y9 H0 ^+ K+ u4 Q( k) Ideclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)* U7 w9 G5 |7 D6 l' j3 J( X
set @root='f:\usr\' -- Start root3 a: e( X, V3 g. p! N
set @name='donggeer' -- directory to find
' V6 T! l1 b  |- }) h$ X  Z1 m5 iinsert into tmp exec master..xp_dirtree @root,0,0
; @+ N+ E) x* K- zset @id=(select top 1 id from tmp where name=@name) 9 V* w& D% ?+ G
set @depth=(select top 1 depth from tmp where name=@name) 9 D! ~+ A% U3 i0 b4 a  F
while @depth<>1 # }/ a: Y* H7 B0 J/ s" E
begin
9 H' m; _  A& ]' N, N! H2 dset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
& L$ S0 `, t' Yset @depth=(select depth from tmp where id=@id) 4 m: H; H& E3 f; }6 @
set @name=(select name from tmp where id=@id)+'\'+@name " z" U, a* o' j2 A; b7 |7 U: J
end update tmp set name=@root+@name where id=1: ~- K# M! [( |
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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