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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句  a9 L& W! }) y$ {% d9 K
0 b) j, n# J: M% m* f0 c
CODE:7 c" _4 Y9 L; `: P7 P

+ |7 X( n% a* @6 [drop table tmp;- t9 ]# ?+ f( J
create table tmp
" ~8 }# G  f. O/ y, _(" v' d* ]- U- H" M1 y
[id] [int] IDENTITY (1,1) NOT NULL,
, e/ f8 H; @9 f/ `. N2 F[name] [nvarchar] (300) NOT NULL,
! ~6 p+ A. u6 C3 D" p( M[depth] [int] NOT NULL,$ a- I! ?2 u6 q4 r5 C! w; o) f
[isfile] [nvarchar] (50) NULL
2 q' J, }; g  o3 I; Q" V% y( \);
( D% `+ j( M; ~* E: ?2 Z3 K. Z+ i' Q3 Z+ v4 E' S
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)8 D7 W( i8 B8 ]3 R, ]# F  t) i, v8 B
set @root='f:\usr\' -- Start root
3 v* \, a2 a1 Z2 A' N. R! \/ Iset @name='cmd.exe'   -- Find file' M+ M) u! v0 Q  j$ a! y
insert into tmp exec master..xp_dirtree @root,0,1--
& I1 R& F  f7 o/ mset @id=(select top 1 id from tmp where isfile=1 and name=@name) * g: X1 \8 j% V+ b+ U
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
+ z& p  {% x3 H/ g3 x( `while @depth<>1
* j4 ~9 C! N  K# fbegin ) e) Y- \- ]7 |' t+ J  ^
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) " a- O3 y: ], g: t6 n. b/ \
set @depth=(select depth from tmp where id=@id)   M7 `' _6 j0 ?2 N7 ^
set @name=(select name from tmp where id=@id)+'\'+@name
" |0 b% l+ h& }/ aend1 O" I0 X6 }6 Z" @7 w& E
update tmp set name=@root+@name where id=1
6 }  Q; S& J1 h+ Oselect name from tmp where id=1
6 g2 ?* `- Z; b) q/ \
% C; h, H# P0 R5 M查找目录的语句
* r, {/ s1 |& m. y6 J4 a2 y+ G1 ^$ p
9 G$ i3 ?" G+ s" |  F
- I/ g. f/ I! \2 BCODE:
2 S5 f2 v' L' b
+ ~  Y# Y; H! z+ o- H
. |* s- U% n/ k1 }& s+ T3 I9 ]drop table tmp;4 A; b# U) M$ h  `3 J2 c
create table tmp0 [) [0 S; w" D! d8 u+ N
(4 u2 b, \3 r5 J6 A. m0 I* t4 R$ ?' [
[id] [int] IDENTITY (1,1) NOT NULL,! p) N$ g9 ^- ]1 B9 k6 `: d$ x. F  [
[name] [nvarchar] (300) NOT NULL,
/ c5 }9 I  m2 y9 \[depth] [int] NOT NULL
6 f# X, b# g  ]/ I. B6 v* N);2 t% y' _* O) X& o. j: q' i6 i- J

; B3 c  l9 K% Wdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)- F2 g+ O9 o/ {! X; l0 }
set @root='f:\usr\' -- Start root9 X* f$ G2 U4 t! N
set @name='donggeer' -- directory to find5 S4 p3 e, [$ b. M7 U, A7 D
insert into tmp exec master..xp_dirtree @root,0,0
, A" v% A6 u+ `9 U5 d3 B" X" kset @id=(select top 1 id from tmp where name=@name) 6 x0 P3 b/ D7 Z/ F
set @depth=(select top 1 depth from tmp where name=@name) 4 s! [+ V8 T# T5 N
while @depth<>1 $ e9 i# s0 k$ }# Q1 q. z; t
begin , E6 ^% ]& j" v
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
6 ^! n' F, U, w8 nset @depth=(select depth from tmp where id=@id)
$ m6 s4 ?5 t6 v! ~( W0 ^set @name=(select name from tmp where id=@id)+'\'+@name ' |: q; G+ J6 v# L2 [! w
end update tmp set name=@root+@name where id=1: Y  J# N7 ~5 G3 k" j
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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