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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
- g; s4 Y1 J; I8 ~( R# h0 X- t
' m7 D" F3 X6 k  a6 d9 ~9 CCODE:. b) w! Z7 v0 h# a
$ t* U' z3 ]- g% I4 X0 [; T$ g
drop table tmp;  M( h$ |9 @4 d$ z  I
create table tmp" n' x) d4 C, A: j5 e& j
(( {% y8 Y, K1 P/ e- Y- J) T- C3 I
[id] [int] IDENTITY (1,1) NOT NULL,
' N( O2 R4 ?* C! K, u0 L[name] [nvarchar] (300) NOT NULL,
' v7 z& `' H* t1 A8 {; V- f$ B2 M[depth] [int] NOT NULL,$ n4 M  b3 \  b$ X$ }; \
[isfile] [nvarchar] (50) NULL# H6 ]6 I# V6 U. N  @
);
$ Y6 S0 x. j' M) n" H
) N* }' K. e( P0 ydeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)6 f3 }# n+ m1 A# g$ a
set @root='f:\usr\' -- Start root  E* }* e. `+ K& W, @8 H
set @name='cmd.exe'   -- Find file
- i' ]3 Y. m6 D* H' \0 ^4 H* |insert into tmp exec master..xp_dirtree @root,0,1--
. L: b% J  {7 S/ X, sset @id=(select top 1 id from tmp where isfile=1 and name=@name)
* w0 j. |4 ~& H$ G* i" Pset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
" N) ]8 g  m" U8 F6 d3 ^while @depth<>1 5 T, V% |* P/ i1 [
begin
8 d( I0 b8 @( `4 a0 l; A  Kset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
. j. o/ x0 V7 t7 r" K1 H' [& Sset @depth=(select depth from tmp where id=@id) / R$ B- F) H! V! P+ u, E
set @name=(select name from tmp where id=@id)+'\'+@name
! h' j6 Q( H+ W0 kend
1 s$ V1 ]6 t6 q, ~. kupdate tmp set name=@root+@name where id=1  H: n( D1 k% H' d& p0 w
select name from tmp where id=11 O9 Z5 E( ^2 a. a0 A! u' G
; j& Z8 B8 t2 U2 k7 f
查找目录的语句
( B8 ?' v& l9 @
5 {  Z: G9 _( Y% O5 R" y0 {$ T; m6 I  q
CODE:! J, g4 _6 f  d) z
9 I7 @3 L7 g* i. y3 J, ^
: h4 y$ S7 f$ w6 ~
drop table tmp;
  l" E% }. a8 Tcreate table tmp) m/ U* ]1 N$ t# }; E
(: F: F% d, T" s
[id] [int] IDENTITY (1,1) NOT NULL,
8 E% G. v$ H( v[name] [nvarchar] (300) NOT NULL,8 _* w& F+ k) L  r5 s5 t) ]
[depth] [int] NOT NULL
' J) B) T. r5 k6 C4 s) b: O8 h);9 U5 T. o  P" [7 T9 u

9 Q7 R) ~2 o/ K" Ddeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300), K( R: S4 F+ D/ t. a9 N2 Q
set @root='f:\usr\' -- Start root  I/ U6 P# F% t7 Z2 F
set @name='donggeer' -- directory to find; o6 l8 Z3 u% J1 }8 h8 `  C7 ^
insert into tmp exec master..xp_dirtree @root,0,0. R1 ^8 I( \+ Z* R* l1 T. @9 ^
set @id=(select top 1 id from tmp where name=@name)
& V) ?5 ?" V) s- Xset @depth=(select top 1 depth from tmp where name=@name) ' B( b* S4 O  K) c" a4 t* C
while @depth<>1
# _3 o7 V" J4 P6 rbegin
% X, V* n6 D" B2 D* E. Yset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
, M' f! F/ B/ n- |set @depth=(select depth from tmp where id=@id) / t6 y& U4 ~, ]  Y% U4 G
set @name=(select name from tmp where id=@id)+'\'+@name
9 O) ~* [' ?) C/ f( B4 Oend update tmp set name=@root+@name where id=1& L9 e8 H. q5 o. J: R3 s1 C- F
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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