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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句3 m5 ?1 R9 F( g. O0 E* l$ ~9 l$ U
- i0 D3 a; P- R) B
CODE:
( e% A% G- [& j/ P
; i* `$ ^: \4 O- u$ W0 M/ hdrop table tmp;
# P& Q% N/ z7 ]$ a* ?* screate table tmp
' A" a( t4 i- s; `(5 ~' v3 y8 b, N8 }; e
[id] [int] IDENTITY (1,1) NOT NULL,( c+ l. b9 Z3 l
[name] [nvarchar] (300) NOT NULL,2 |; [( ~( e+ D2 h9 R' P; Z) r
[depth] [int] NOT NULL,: ]- X+ X7 v& }2 }, t4 [  g' P! L
[isfile] [nvarchar] (50) NULL
+ |1 w0 R: A+ C+ ?' x+ \$ l);
" e3 E, S2 ^% y0 J; r  Y
$ L/ \& X6 ~/ \) m% m) c: Zdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)8 S+ [+ Y! N+ m5 o3 d& e: }
set @root='f:\usr\' -- Start root! B! m3 h4 t% M* j
set @name='cmd.exe'   -- Find file
/ N" ?# x2 G% K3 n$ W. Binsert into tmp exec master..xp_dirtree @root,0,1--
# [/ {" e  {2 ^) z6 @set @id=(select top 1 id from tmp where isfile=1 and name=@name)
# C& s# F( R: K- Hset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
$ H, p4 K) h1 S& b# gwhile @depth<>1
& R4 R" e$ [- `. L8 g( j$ M& G. lbegin . m7 R, ]2 ~+ J" R  T) x3 s
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ( u( ~- J7 R  [6 N+ J- @4 @
set @depth=(select depth from tmp where id=@id) - J3 @2 i/ \; D  c6 i
set @name=(select name from tmp where id=@id)+'\'+@name
: `& n# b/ C. D& A  ^; E% pend
8 L, Z4 t2 o0 H8 h+ x9 eupdate tmp set name=@root+@name where id=12 T2 H  \$ t) @! N$ U8 D
select name from tmp where id=14 T, e. ?: \4 O1 u' U  m# ~+ y5 ?

2 |: v% V4 h7 n7 Z5 X查找目录的语句0 x, x2 [; M1 q  ]# C! L4 b

  @: V4 X- K) E; s; t9 a# W3 b0 b- a  c+ d4 Y; F: s, h
CODE:# K, M. f3 y# |# Z& Z0 P' P) A
1 ~* N0 R  b( Z3 z2 R
7 b* @- \6 W1 g* R' G
drop table tmp;8 u) K) X8 d7 V% L  }. Z5 \
create table tmp
2 y7 O% X  ^2 U(& d7 j; r( b9 @7 ?* R
[id] [int] IDENTITY (1,1) NOT NULL,
. ]% d4 |$ M$ D( u9 [[name] [nvarchar] (300) NOT NULL,
7 C; f) L+ s/ F9 ]( \5 ~8 m[depth] [int] NOT NULL
. P9 Q( H& C# G" k4 `);1 Z( V/ J% Z3 k+ l

- i: E  U6 O7 ^$ }" m$ hdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
+ K( J. q/ T6 M7 L! Hset @root='f:\usr\' -- Start root+ m" K. k* }- k9 c6 C$ a) v* s
set @name='donggeer' -- directory to find8 c) p  C) z- w
insert into tmp exec master..xp_dirtree @root,0,07 n( d0 f4 p5 l  Y2 h. z
set @id=(select top 1 id from tmp where name=@name) " }1 V/ q) z) S5 y
set @depth=(select top 1 depth from tmp where name=@name) ' F* T! I, E7 `2 G. d& d6 a
while @depth<>1
* w! @6 j4 E# wbegin 4 u1 A9 F  ]) W0 j% p( d, ^- a" F
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)* u5 z' r( _. e# \
set @depth=(select depth from tmp where id=@id) $ Q) J8 E2 o3 j+ y" ?
set @name=(select name from tmp where id=@id)+'\'+@name
9 Z1 s" C9 y4 j; wend update tmp set name=@root+@name where id=1
6 ^9 b  L7 R) Q5 mselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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