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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
+ c  A* \8 G8 }. Z6 V2 j1 U4 ~5 L; x* a  K' n  t1 N; x
CODE:( \6 E! a, e' Y: ^2 A- C- p4 y
; G7 x+ x  D! u* H
drop table tmp;( y. \/ d( m( Z1 ]
create table tmp
' }+ ~9 F% M' A(9 [  R' O) T  u9 b
[id] [int] IDENTITY (1,1) NOT NULL,
6 z6 w  t+ ^6 f& T* o: N4 }[name] [nvarchar] (300) NOT NULL,. b  z: C, H  m3 L2 t' M' D
[depth] [int] NOT NULL,5 R" s6 R0 O3 X  O7 q
[isfile] [nvarchar] (50) NULL; S  E7 P- d* m+ x' i- k' F
);
# }  H5 a! R" |' N8 x+ ^
. M  G, H# s# }& j! D; tdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
' w7 x0 S5 ^- b7 j  Z# |7 qset @root='f:\usr\' -- Start root, p' x" n$ S5 _8 K' R
set @name='cmd.exe'   -- Find file
" R& C; S7 Y7 y& z" h4 vinsert into tmp exec master..xp_dirtree @root,0,1--: e9 a! N& y9 a7 j, V
set @id=(select top 1 id from tmp where isfile=1 and name=@name) * {+ C' c  h1 H$ f) D* w/ @8 p5 D
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)! Q0 R$ q/ D5 b* d
while @depth<>1
: x3 p8 }. e& g( Y& E# H0 p" abegin - N; j; O" D# |
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
( d% u1 B0 m/ R& F& A0 v1 M* Pset @depth=(select depth from tmp where id=@id) 8 n, b8 B9 A! Q4 I9 U' a% ~3 G6 \
set @name=(select name from tmp where id=@id)+'\'+@name% }' B# Q% j# W2 r: u
end8 {* r# }+ n, @) L! ?
update tmp set name=@root+@name where id=1% j2 j3 R# P7 T( }* e# }6 F6 S+ [
select name from tmp where id=1: ?# `) k9 T9 h) U
0 ]1 N$ m3 K2 X$ d/ X
查找目录的语句& C1 L' h2 I5 ?! K% j( q( z
9 ]# g. z9 T7 P2 g( {% l! s

5 d. O! x, e/ s1 T! ]CODE:2 n9 w) g7 F" `# _/ Y
/ ?5 N, r4 k/ y

9 n1 ]2 r2 q4 Jdrop table tmp;
1 k' x: p0 j* P  H5 V  ]create table tmp
/ t+ }4 C( z6 t- W, M(
( {7 d4 M) Z: l5 t3 t  u[id] [int] IDENTITY (1,1) NOT NULL,
8 ~0 t% X6 Z" U0 L# ^- e, ~' g[name] [nvarchar] (300) NOT NULL,# l2 F  l0 @8 U9 Z8 B- Q
[depth] [int] NOT NULL3 m  c# B& e+ r3 M/ |. G
);: E8 |% k7 V: C; }

: z0 J0 s& y. y# f: fdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
$ |# f- @/ G( Hset @root='f:\usr\' -- Start root& ]& U; q. ^8 G
set @name='donggeer' -- directory to find
% {5 q6 R* ~8 b3 T: g, Vinsert into tmp exec master..xp_dirtree @root,0,0
; _7 Y( S9 R/ l+ g' F; D7 {1 \* Pset @id=(select top 1 id from tmp where name=@name)
5 p8 ~! t& m$ ?9 }1 Qset @depth=(select top 1 depth from tmp where name=@name)
1 S( ^/ Q9 M9 A( ?* N* dwhile @depth<>1 ' f/ I6 }( J, E" [
begin ( [3 r0 m3 `1 ^  a) q( W
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
( b$ I/ N! q0 [: W% w$ lset @depth=(select depth from tmp where id=@id)
8 u( m3 w- x( H7 a# M+ R' T+ X$ Bset @name=(select name from tmp where id=@id)+'\'+@name
  r. w0 U  t- Kend update tmp set name=@root+@name where id=1/ t: T/ w  K6 X+ B9 [
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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