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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
: p5 O5 r; m! a; _' ^" o- Z! }+ C
- v  \' O& i( G- ?. gCODE:
. m! v. O9 ?" R: Y/ L) q9 q/ |9 N1 X! i: P( B3 b
drop table tmp;
7 L$ _! b8 E4 y  I) l# _create table tmp
$ {) n# ?  F/ f$ Y(
  Z" I# k6 V! f" p: n; F! d! K[id] [int] IDENTITY (1,1) NOT NULL,/ a+ G% @! @/ C
[name] [nvarchar] (300) NOT NULL,5 q. E5 }5 y3 K) p6 j& V
[depth] [int] NOT NULL,* O/ N; Y9 h2 h
[isfile] [nvarchar] (50) NULL
  o0 Q$ \2 E: P; b; X);
$ k( k& P( ~& N: x% K! g" p9 ^# \! h4 X
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)2 |& [  L$ [" \  u' P0 ^
set @root='f:\usr\' -- Start root
! h0 f% i5 q9 e1 n' U! fset @name='cmd.exe'   -- Find file
6 }' ~7 W& ^2 z) l5 R& ?2 m; }insert into tmp exec master..xp_dirtree @root,0,1--2 p% B3 P! ?1 E$ F, ]/ c7 [% a
set @id=(select top 1 id from tmp where isfile=1 and name=@name) ' h3 f' f# q( j  y# o9 Q
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)' B2 b: E- U1 V
while @depth<>1
& ]% ]& r4 ^  G6 |begin 3 W$ b% `% p0 f3 {* @
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ! u, I* Y& r/ `- M' K
set @depth=(select depth from tmp where id=@id) 3 u  t( j$ Z! o9 Y6 V
set @name=(select name from tmp where id=@id)+'\'+@name( N) d9 S2 y5 q
end, o. r+ Y; Z! [+ u1 F2 V
update tmp set name=@root+@name where id=1
. v+ o4 W- k$ x4 vselect name from tmp where id=1$ |* F% H" F+ b$ d+ [. k
7 W8 K7 S. \# m" O1 W7 s. g8 x. {
查找目录的语句( G9 Z6 C% Y5 N! g
8 x2 Z' `' w3 P4 D* F8 h1 ~8 |

6 X) p0 X5 i* t0 TCODE:* Y( o9 s3 b4 V. O2 \* W
* T5 J. r0 G  i, Q# Z$ @1 r

5 s7 o3 O) k2 T0 [( ydrop table tmp;
: ~. z2 n$ w) `3 ?" O* c2 A1 Acreate table tmp
/ S% ], l" L1 H, R1 e6 ]' j; F(
5 @6 X4 e3 {- {$ I[id] [int] IDENTITY (1,1) NOT NULL,1 y- j. f% X$ Y
[name] [nvarchar] (300) NOT NULL,; J! f$ [3 y) b# e
[depth] [int] NOT NULL- K; f: h: b" ?! o
);
5 S( }' Y' Z- c9 o2 R3 C# l& d
" [% _- Y* [0 I( Pdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)/ c- O, v6 J4 c; y+ `, o
set @root='f:\usr\' -- Start root
: L: N3 N% q5 t. f7 _9 `; Eset @name='donggeer' -- directory to find
. w% ^( T" o& l& V) w. L& P6 D1 _insert into tmp exec master..xp_dirtree @root,0,0
( ^- T; o/ v$ G9 v* B- V, z, \) D8 ^set @id=(select top 1 id from tmp where name=@name) 2 b4 Z1 L3 B/ e8 l( H
set @depth=(select top 1 depth from tmp where name=@name)
, J8 ^2 N0 a' ]: C, lwhile @depth<>1
. E/ q8 A6 J9 J8 {- dbegin 8 e  s: B& J; d% ?
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
4 [: W- X! k* s' @set @depth=(select depth from tmp where id=@id)
! f1 `0 h) K6 Z, W# h# Tset @name=(select name from tmp where id=@id)+'\'+@name
! a+ A2 P+ q% Q4 Xend update tmp set name=@root+@name where id=1
9 v, v+ F+ ?2 mselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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