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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
查找文件的语句
0 J( J* M& d0 Z* ~1 S3 D7 |$ O  U$ P0 ^8 D4 x1 U! K! |
CODE:
7 E- g/ \% D. Z0 a6 y. A0 G/ Y: _0 r- X5 F- M  H% M
drop table tmp;* f1 U* ~; y9 K
create table tmp
: w% W3 G# ~+ I) i0 R, u& ~9 [(
3 }5 X# Q0 e2 h) F6 J+ h& m7 {[id] [int] IDENTITY (1,1) NOT NULL,
0 `7 h; I% `: k6 g- w[name] [nvarchar] (300) NOT NULL,
* J7 v$ p( r2 Y* w7 q[depth] [int] NOT NULL,' K# R. F+ h$ Q* c. N" f
[isfile] [nvarchar] (50) NULL% j* K1 r3 S) c9 ?3 `
);
9 v- d' E* i, ~( w9 `5 H1 d5 x" M# j# ~' @- v, Z
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
/ b* `" I. k  }3 S; r4 E# K+ }set @root='f:\usr\' -- Start root
; i. ]1 r2 x* r3 z- V5 {set @name='cmd.exe'   -- Find file4 U( L. I5 B+ R4 C) H0 V
insert into tmp exec master..xp_dirtree @root,0,1--9 T% K% U& _! t" W+ E3 [8 s" L
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
6 d/ C" _- s. g( c2 {0 dset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)% {  S% J0 d# K
while @depth<>1
: D1 [2 W5 h" q) rbegin
/ t* G1 y. p% v$ \1 e3 f6 Vset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
/ ?  ^+ ?8 t! y: G5 ]" a9 Kset @depth=(select depth from tmp where id=@id)
4 V5 }+ y7 `* W* e$ K1 Z9 E3 c7 nset @name=(select name from tmp where id=@id)+'\'+@name6 a2 `) D6 ?( m) [9 _& K! _
end! F" b  J5 U" C0 O
update tmp set name=@root+@name where id=1
& i0 k$ S" J( Cselect name from tmp where id=1& l1 m- [1 A8 j

+ [6 E, F" }) u! x# Q# V* u6 F+ ^查找目录的语句
- b* w$ H( B( Z& [2 v9 s$ ~2 [3 L/ [9 {8 U7 V
( Z# ?' O5 ~9 Y8 v2 V
CODE:
5 _( Z' ?) A# m" v8 m; L* Q( w/ `3 ~& T) g" j' s

7 f+ J) i1 X+ D& Bdrop table tmp;
: \! \. t5 B7 W9 B2 a5 vcreate table tmp/ n- h, D% d& W' n
(: R5 t$ I  y$ y* A% p
[id] [int] IDENTITY (1,1) NOT NULL,
* ]- [5 d) V8 Y% R4 b! R[name] [nvarchar] (300) NOT NULL,
4 y( V# l; @2 H1 }6 j/ D[depth] [int] NOT NULL7 a/ ?. Y6 d7 w; D
);1 f) F, Z; u1 R+ \" \0 ]1 W

1 {) E+ h1 W% S; }" T8 adeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
4 Y  M) r8 v4 p8 w2 N4 aset @root='f:\usr\' -- Start root1 l- v' I+ Z/ `4 n5 N4 j
set @name='donggeer' -- directory to find
5 ?$ E( @( P! m/ Cinsert into tmp exec master..xp_dirtree @root,0,0# J% u9 U+ q, w  P4 s( X
set @id=(select top 1 id from tmp where name=@name) % p0 }  R5 x& h8 j# F; Y. i- p/ w- C
set @depth=(select top 1 depth from tmp where name=@name)
8 {2 T) ~/ A; K" t' ]& z5 ?& e. Kwhile @depth<>1 0 ]7 `+ Q* T, _$ u0 ^0 a& f
begin
. a) m% f$ O# x9 z; o3 f0 i4 L. [set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
' u: o$ }7 `2 Hset @depth=(select depth from tmp where id=@id) 4 I; [$ ~; K4 X+ c
set @name=(select name from tmp where id=@id)+'\'+@name
/ ~5 o- o# ~5 X' oend update tmp set name=@root+@name where id=1$ G6 J  X1 O6 B5 e
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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