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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句4 W# u. t$ p$ ~. ]8 V: Z% P; s- P7 Y
$ K2 H' }9 W! C8 O
CODE:
7 q3 v* k5 F2 |$ _' v8 m# d* m
% ~$ [! i/ v+ R* b: T; ldrop table tmp;# U' E! R; H6 ]3 c8 `, H
create table tmp2 B, \( ?+ K, z8 q% z3 v
(
* A2 m1 a! p6 y5 l6 z, V[id] [int] IDENTITY (1,1) NOT NULL,
. X5 d2 F+ H& G1 g9 t2 m[name] [nvarchar] (300) NOT NULL,0 |( ^% o3 r% }5 f8 o
[depth] [int] NOT NULL,' g. s/ Z# k# {, C8 I, ?+ I
[isfile] [nvarchar] (50) NULL
; {7 p! @/ A# H);8 v! [6 b: i! f. n: x1 N
- U6 j! Z2 e( a# S3 V4 i
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
/ ~( l2 {. W- Gset @root='f:\usr\' -- Start root, P: R$ l* y9 E0 o1 Q4 I
set @name='cmd.exe'   -- Find file3 e; P5 \( v  H) V6 |! s2 {4 y
insert into tmp exec master..xp_dirtree @root,0,1--
+ l  [3 H/ j1 i! t6 m1 {set @id=(select top 1 id from tmp where isfile=1 and name=@name)
) P" Z" ~$ H5 F" C; Wset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
0 i" N$ C& v/ Y/ \while @depth<>1 " k4 U- i3 e2 I! y
begin % w; `9 M. y9 c4 K0 b
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
- ~/ G# u  q% @: I4 O' E4 k  b9 iset @depth=(select depth from tmp where id=@id)   v! x% P4 k; a4 @6 ~% v
set @name=(select name from tmp where id=@id)+'\'+@name
0 B' ]3 v, M# o1 l1 J7 T- \end8 j" ?3 e7 T% ?0 P( k4 Y
update tmp set name=@root+@name where id=1
0 L3 {5 S# E% [$ j) n( r: a# m+ q$ vselect name from tmp where id=1
& h5 F) x& y  J. W8 S9 q  \/ z( |. M- h9 n; E, h
查找目录的语句
+ D- B  U; C, D3 G" f' g% ^1 _& @" Y7 U7 \. ~6 b5 w0 ~$ I

( r! e2 Q: I8 r. \% hCODE:. m% n, ]" d* T
; r6 t4 T' J3 ^3 ~( z* |4 x' }
& K$ @! ]/ R2 g7 e% t" N/ J
drop table tmp;! C" X6 q1 X. L7 ~. w8 N! E# X" W+ Y
create table tmp0 R  c5 k4 V* n, d3 s
(9 K% u- z, w9 }) [" m% @% l
[id] [int] IDENTITY (1,1) NOT NULL,  q5 J1 B# P( [* @2 t; z" e' z
[name] [nvarchar] (300) NOT NULL,
' ]1 {5 ^% ?* p[depth] [int] NOT NULL* t1 N4 ~* C- n; h' ?
);
+ D5 [& \" ], U
( A1 y- i: Z% K3 D% U4 Tdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
  S* t/ [; o* k5 w1 C8 e' iset @root='f:\usr\' -- Start root
6 m: }$ X- ?6 a- W- [" n, v2 nset @name='donggeer' -- directory to find
+ u* [! q; k; K4 q) D* tinsert into tmp exec master..xp_dirtree @root,0,0" l9 x. I+ x* m) ~% K4 _. g# w+ u
set @id=(select top 1 id from tmp where name=@name) - y0 B4 k5 K+ ^/ ?7 D
set @depth=(select top 1 depth from tmp where name=@name)
% S. R  i0 _% L5 P% [0 F; l9 S* nwhile @depth<>1
6 r5 x' y1 q7 Sbegin , U) F9 m6 c1 `
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)' u6 d$ ^& X' ?& b7 A9 J. A
set @depth=(select depth from tmp where id=@id) % c4 A1 H  w* V; U  T
set @name=(select name from tmp where id=@id)+'\'+@name
5 w" `1 _. X, |( P3 }1 ]" I; |. mend update tmp set name=@root+@name where id=1
6 h- Z2 {/ f+ `; a* gselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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