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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
& i( J8 o+ _1 G
! b3 |/ D9 I4 o. hCODE:/ e2 S2 `/ o. v3 t: K- A: r

7 s: n9 I. E+ y8 Xdrop table tmp;+ y- O; Q% O# T- N& s$ [2 m
create table tmp
% R% e5 e' @! u" i(
) N* x8 r: C$ ~1 m[id] [int] IDENTITY (1,1) NOT NULL,
2 M$ ^' n% p7 {, T0 e7 [[name] [nvarchar] (300) NOT NULL,
8 a' }. \( {; w/ q3 ?' r[depth] [int] NOT NULL,
' \+ V. y$ [& ]: u8 a! Y8 e3 j$ V[isfile] [nvarchar] (50) NULL, F1 }0 s1 }- [; G; r
);
+ b* [( S. S' r, L! a6 _$ j& d0 a8 t& \& x; m. a
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
* F' q" @9 ~9 n% Xset @root='f:\usr\' -- Start root6 l8 ^% P, g2 @+ c/ h
set @name='cmd.exe'   -- Find file. n, t1 D1 i) n( ~6 g/ z* C
insert into tmp exec master..xp_dirtree @root,0,1--
6 g, l9 i% T  U- Rset @id=(select top 1 id from tmp where isfile=1 and name=@name)
( R0 |, K5 O" S, ^  r9 Vset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
2 M0 e, q% q; x+ K. u$ ?7 qwhile @depth<>1 ' v( x5 A* j2 p6 w. p, j6 ^# M& V
begin
: g% \+ A* [7 K/ vset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) * K9 j8 E; `; d1 H" ~
set @depth=(select depth from tmp where id=@id)
5 }. b/ G2 [" `+ Yset @name=(select name from tmp where id=@id)+'\'+@name
5 {! {, E% N* h/ P! d0 l/ n/ Dend, U- `+ S% p" p8 C1 X) P3 ]
update tmp set name=@root+@name where id=1
2 ]7 s% S1 z& f+ a7 H2 ~" l, Lselect name from tmp where id=1  U2 T+ q+ E, F# D. a# s/ E
5 ]0 I6 O. ~# P* m* J$ {8 ~( S9 Z
查找目录的语句
& P# H; ?5 u- w: N
6 g- y. t( ^5 d; W: @2 j* l0 `% m7 l6 I: ?" k0 M
CODE:+ j2 Z" v! E% j/ i
* w" ~+ _, L7 M" F% K
& C0 i$ W" T2 C- ~! [
drop table tmp;  t7 p( s) c' I, D. B; o( K
create table tmp
- t7 b; O) j. v; t7 q(! z7 t0 K5 T! ^
[id] [int] IDENTITY (1,1) NOT NULL,
: X5 h" m' Z- f1 R[name] [nvarchar] (300) NOT NULL,
8 f* t  t% [2 X' m4 M" K2 u- c[depth] [int] NOT NULL. r5 x( m" C/ p. h6 W1 {+ g
);
. |' K* F; {5 ^; ^9 I  V/ g( B2 t- A/ L
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)/ p: G; [' d( P8 w- K" K( j+ r
set @root='f:\usr\' -- Start root0 j. T  m# n) v1 }
set @name='donggeer' -- directory to find
* t1 W3 z1 k" L$ p! pinsert into tmp exec master..xp_dirtree @root,0,0
! y' L9 T9 r% T- H% n) O$ Uset @id=(select top 1 id from tmp where name=@name)
2 `. n; _5 ^1 j, G7 j* N1 Qset @depth=(select top 1 depth from tmp where name=@name) 4 v6 t3 f- Y$ I+ d2 X
while @depth<>1
& u" `- Q# n$ r  h9 q6 gbegin ( h! s4 p9 D. N! E6 V
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
9 H1 z) p# C% q# E7 ]) iset @depth=(select depth from tmp where id=@id) ; F) w* P0 R4 H7 N3 J' d, b$ \
set @name=(select name from tmp where id=@id)+'\'+@name   |5 z  M/ i( K: [$ l9 w+ M
end update tmp set name=@root+@name where id=1! S- |  b% c. d
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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