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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
4 k8 T5 q- ~' o, R3 I  ?2 v
7 L& S: X2 m1 Z, d3 PCODE:( }' v2 L% r- E1 R5 R& |

! E3 q! ^( @- L7 adrop table tmp;
% W- c) d2 \$ d: g3 icreate table tmp6 n& l+ E1 q+ g, @0 w4 W
(# o0 `5 Y4 c% h6 Q8 v  s" L) f
[id] [int] IDENTITY (1,1) NOT NULL,
( l2 o0 a& {$ B1 V" c[name] [nvarchar] (300) NOT NULL,' _7 r; L- d9 F; W/ t
[depth] [int] NOT NULL,  v/ G6 G- Y. T# a0 Y/ g- ?
[isfile] [nvarchar] (50) NULL8 J, e% @. }6 p1 e! x! a
);, j. [5 G& `5 }& D4 s2 [

5 S0 C4 y6 o: v1 D! c" {declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
' D2 F1 u5 i, c2 |% e9 Kset @root='f:\usr\' -- Start root
* ~0 e7 V" V+ W+ J4 kset @name='cmd.exe'   -- Find file4 p, \5 ~" w* o& G+ P0 }) P1 ], t7 z
insert into tmp exec master..xp_dirtree @root,0,1--0 r) {4 V! u9 S8 |3 N: g: G
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
7 c- P% E! T* I% [' @% B# A' zset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)0 X* A* Y* W" a% _! A$ X
while @depth<>1
) o- i* m" t% F8 H' u. Mbegin
8 I* \$ c5 w5 U" a! hset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 4 c2 Q9 }6 X$ _7 g  i7 [
set @depth=(select depth from tmp where id=@id) . _: h, Z8 P/ u' _3 g9 R
set @name=(select name from tmp where id=@id)+'\'+@name
* z! O. ^4 q8 i$ M8 j8 Y; U0 mend
' Q8 Z  V3 O  a1 ^+ k4 yupdate tmp set name=@root+@name where id=1
7 {1 \  G/ T; {( t' sselect name from tmp where id=1) x7 p% Z- ^7 J) F! o5 D1 c0 A, E, X, O

, q) e3 T! i0 V# R1 b, r* O查找目录的语句
- H0 d1 k5 y/ p) L8 m
3 {( \5 |( C) |6 H0 P; Z% u$ [  Z1 \, v: e# e  j. a; S
CODE:3 M5 F% p' d  \$ K- F( e" E

; p2 z) C/ @/ P5 T" z3 b; S2 E  j
' b. f6 M5 N2 X* h; pdrop table tmp;( A8 ^: t! \! T* |6 c7 q
create table tmp
- k! P+ f4 d8 n$ t(
: Y% U- g4 K7 W2 R+ s; o7 H/ r[id] [int] IDENTITY (1,1) NOT NULL,
6 W) G, H$ n1 [% q[name] [nvarchar] (300) NOT NULL,0 R7 ]: r. U/ Y5 s& i. O! ?( _
[depth] [int] NOT NULL
# W: \6 w! A4 m: D/ g);
6 ~2 a8 O# m0 L, x
, _. Y2 w* n- o+ ~  B- xdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)  z! o# E' b( B$ d( h- y
set @root='f:\usr\' -- Start root! o* V' i" Z! E3 k- K: b; e
set @name='donggeer' -- directory to find
& O9 }& q; H# ]1 Q) ?insert into tmp exec master..xp_dirtree @root,0,0
8 l7 Q7 Q, L/ M  Tset @id=(select top 1 id from tmp where name=@name)
8 R0 U9 D5 ^9 x" Iset @depth=(select top 1 depth from tmp where name=@name) 2 e- @6 T5 m/ T( f7 l
while @depth<>1
1 i% O% D/ a! i, v4 ^' |  mbegin 5 g1 q% w9 v4 g$ O$ W( b
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)8 u/ \* D) i1 f) A
set @depth=(select depth from tmp where id=@id)
( u' a. y3 x2 y+ l" X( Zset @name=(select name from tmp where id=@id)+'\'+@name 9 d: P+ d/ f, p# \6 G5 x, m
end update tmp set name=@root+@name where id=1
9 O  A& X3 B) Iselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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