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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句. G; o# h1 \8 m: Z1 u
. v: e) Q: H' u3 E1 z1 g# |8 e1 Z
CODE:
9 D" M' z) [* N7 v$ ]' c8 @
+ W2 L& A* z" P# z. S) q  ~5 _drop table tmp;  j1 f  u' U, P; G" }% a; U
create table tmp
; F7 @/ [; {3 p3 s9 |) v: s" R(
" g0 \. f. e4 M$ x0 A! c! y[id] [int] IDENTITY (1,1) NOT NULL,
: `3 q; v4 G  L% o: F[name] [nvarchar] (300) NOT NULL,3 L" ], c5 r" c# w
[depth] [int] NOT NULL,
+ k/ g( V8 J( x  \6 k[isfile] [nvarchar] (50) NULL9 d- N6 f7 q5 |3 I# \# w9 e! x
);
" h; w# S# @# u. u. P2 o6 d4 W7 m2 A2 Z' K+ R8 e3 W
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)* j/ o- X$ D$ D, j' |
set @root='f:\usr\' -- Start root# c& [1 o/ e: _
set @name='cmd.exe'   -- Find file; w* R. v! G$ i0 N& X) S7 F2 y% K
insert into tmp exec master..xp_dirtree @root,0,1--
# t$ e! \/ H0 o/ P3 g4 Bset @id=(select top 1 id from tmp where isfile=1 and name=@name) ! n: q4 F1 X) ]
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
- h; S. m0 m( z) R9 h+ Hwhile @depth<>1
, ~1 n% w6 ~, D6 H$ t& ?begin : c; O0 Z8 I: F; d$ `' E
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 4 G% ^# z; i$ O$ |6 ]0 m! e" h
set @depth=(select depth from tmp where id=@id)
: W- z, Q' U5 ]' T- n. ^( hset @name=(select name from tmp where id=@id)+'\'+@name' D* I8 R2 }6 ~8 ~. R9 @
end! D, S9 P: V! K
update tmp set name=@root+@name where id=1* @7 O9 j3 n" C7 `2 K% P: Y
select name from tmp where id=1
2 ?' p) a+ ?) K( G" M$ {6 \  N2 K6 p3 K  l& r* _. P% N. f
查找目录的语句/ S3 V8 T( v# d' `
; Z# n% ]/ q7 o9 r! v% u% t) ~* I" r

, g5 f9 d8 Y1 r. R3 f* c  W+ E$ LCODE:# [0 u' p  v' w
) y) e1 {+ N8 S# a  R
$ s) E  K) T% V* u, K/ v; Z. ~
drop table tmp;
7 e/ s  A' S6 Y0 ?; F  Bcreate table tmp8 v9 [& c" c  A4 K0 H- d* ]. N
(' P5 Z# ]" i# d+ ^& Q
[id] [int] IDENTITY (1,1) NOT NULL,
! F7 e1 x4 \; ?) r$ W[name] [nvarchar] (300) NOT NULL,
2 F; _' `" F( w1 ^' r' V! v[depth] [int] NOT NULL
  s$ a5 l$ P, ]( l, ~3 G3 S);7 m- E3 t0 S! h- G3 C- a$ D; M0 r
% c6 K% [2 U# A- L
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)9 _6 U7 Y; L) ~0 K$ d; M/ b
set @root='f:\usr\' -- Start root
7 H+ b3 N8 G5 f9 Y9 }0 j, n- A# W! eset @name='donggeer' -- directory to find& H2 q& w7 s5 l/ H9 e
insert into tmp exec master..xp_dirtree @root,0,0" K$ s, ]: I1 \# {) z
set @id=(select top 1 id from tmp where name=@name) + F7 }- ?+ T: y, R- z
set @depth=(select top 1 depth from tmp where name=@name)
; e4 }, N: |6 N$ Ewhile @depth<>1 * k4 e( z; z* t; y7 r2 v0 `7 S& }' u
begin
5 y+ J4 k2 H' Y+ x3 e; i% Rset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
, S. D! S; V8 Y( h( r# @set @depth=(select depth from tmp where id=@id) , m1 {! q) m; p* C" R: Z
set @name=(select name from tmp where id=@id)+'\'+@name 7 G& }- {9 x0 z+ k( e
end update tmp set name=@root+@name where id=1
6 a* L+ b+ n, |. k! S: _6 r9 P/ Kselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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