找回密码
 立即注册
查看: 2776|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
  |% _( b: b7 O( e3 P1 _* w! c' }% Y  a+ d' ?; L" y9 g
CODE:2 d8 O) _) q) {1 O& \7 S
, z- ~0 q0 P0 l3 q! c+ A( I
drop table tmp;
2 P5 @8 ^5 _9 B/ ]. Fcreate table tmp
2 ^* q; p' }8 i2 q5 |* W) Q(
, y& ]; o/ Q+ T$ {" B' b, X[id] [int] IDENTITY (1,1) NOT NULL,
5 |- k0 k) h- j: M5 O( J4 s" S& }[name] [nvarchar] (300) NOT NULL,( y" H) `, n& b! a6 s
[depth] [int] NOT NULL," g8 H4 A5 L1 Q& Z2 l$ f0 B
[isfile] [nvarchar] (50) NULL" U' `7 A6 O  q- o1 _; |
);0 ], }' u) f( s
$ A2 Q8 J9 T' ?5 j0 R+ W
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)" ]: l( }* ~6 O: v
set @root='f:\usr\' -- Start root
1 o8 f9 c# E( b) n0 k. E* G1 n+ X: Cset @name='cmd.exe'   -- Find file2 B6 v  A+ K$ x4 `9 C3 H
insert into tmp exec master..xp_dirtree @root,0,1--1 `4 c" y! ?$ E1 c
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
+ q; i' y, Z1 X4 P) sset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)- k9 d8 d, N# d! Z. [# y  w" Y
while @depth<>1
7 [0 ]) V/ D7 u& B: B- cbegin
6 m/ o# m- F% G" a( \# P7 @0 Fset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
# p) Y' m+ q; r- bset @depth=(select depth from tmp where id=@id)
9 f! a& c, i& U, ~6 p: sset @name=(select name from tmp where id=@id)+'\'+@name
6 U3 E! [2 x" ]; W0 s8 g; n# Nend
; b) x- t) _" X) hupdate tmp set name=@root+@name where id=1
# `8 j+ H2 @/ X. O& r" rselect name from tmp where id=1
, c) U  b  e" |7 U8 u3 A; l  s$ j8 R5 l
查找目录的语句
1 r" s' b, y0 J: @! M: s; X. v  f- i+ F7 g1 L# q

) x' I5 m" ]" L. x! ?: Q0 TCODE:. T6 m0 I. h1 }) L( h. p! Z8 o/ x

( E8 g. W2 g9 v  P6 R8 T$ O" R
. w' t* t$ X& o: S( mdrop table tmp;) L8 ?1 Y- E4 P& I% H
create table tmp( C5 q' O( ?! w% T/ e# M
(6 t( |, |) |' L7 c( L
[id] [int] IDENTITY (1,1) NOT NULL,0 z% G, V% a3 G3 H/ K1 y
[name] [nvarchar] (300) NOT NULL,8 T9 r2 A7 L' o; @. j9 c
[depth] [int] NOT NULL7 ?5 a0 M- A) X! k
);
( I1 G: j8 e6 p5 m" {7 f# i3 W: m2 C7 u/ n
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
+ x7 l# i! Y, J3 Jset @root='f:\usr\' -- Start root/ p% E& Q' r2 D3 a, Z
set @name='donggeer' -- directory to find2 }& B7 u8 ?% U  b3 @
insert into tmp exec master..xp_dirtree @root,0,0  ~* f1 @' b7 O  ?
set @id=(select top 1 id from tmp where name=@name) + s2 Q# h3 P! p  a8 |
set @depth=(select top 1 depth from tmp where name=@name) & V9 s0 s! d# {' W
while @depth<>1
( R6 L- E5 ]5 o( m  T" bbegin % v5 L; p8 @% ~3 u+ _- P8 `5 }. n
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)5 p( @5 J: e* N  F' z
set @depth=(select depth from tmp where id=@id)
6 L* J1 ~: x/ G# e# bset @name=(select name from tmp where id=@id)+'\'+@name + h. Q' y  O4 r# n! Q
end update tmp set name=@root+@name where id=1
, H( i/ j# u+ z  P/ lselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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