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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
4 l+ X+ v" i5 P6 }. Y/ [" J
4 Z4 j* i0 |+ S! D; o  }" R! c* bCODE:7 y$ o0 O* S, [7 W9 l
- g' O. Y$ Q) K% x+ m0 f) C
drop table tmp;
6 O( m; z1 o! [# @create table tmp/ W& P+ X# q/ e' g  N: s
(3 Z$ [: g% @# g  g7 ^
[id] [int] IDENTITY (1,1) NOT NULL,1 r( }1 B% {- }, i+ Z' ^
[name] [nvarchar] (300) NOT NULL,8 G4 }. G, l  G+ C1 e5 ?; ]
[depth] [int] NOT NULL,
9 Y" y7 Z% Z, s" K2 m3 ?9 G[isfile] [nvarchar] (50) NULL9 W8 ~" s+ E& q- u
);, ?% n$ |. l3 r* l3 W
) ~6 h0 J; f2 ?
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)* N0 _$ J" ]9 y6 I' r- w" m" H
set @root='f:\usr\' -- Start root
" `$ G4 t* |. Q: Vset @name='cmd.exe'   -- Find file/ k. Q" }& N# v. X6 {
insert into tmp exec master..xp_dirtree @root,0,1--
& d9 i4 d+ q# L% F  X3 b( ]/ x: Nset @id=(select top 1 id from tmp where isfile=1 and name=@name) . P1 O6 |" t/ x+ _' _% v
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
- O% u5 D# T+ |! c6 rwhile @depth<>1
: j% d* L! ^/ Y/ K' m" |begin
5 ?% s6 w/ h" G8 L: Tset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ( S) T0 _) D) l$ a( {. |
set @depth=(select depth from tmp where id=@id) 6 C5 t5 g+ s" f
set @name=(select name from tmp where id=@id)+'\'+@name
3 @. P/ C0 g) \6 Hend
7 s! Y) X+ |8 G" T& `2 o0 pupdate tmp set name=@root+@name where id=1
% R+ ~$ a" ^+ t+ [- tselect name from tmp where id=1
5 U3 _% O* r4 ]' ~
7 x5 |8 h! j% v$ o5 O$ s8 _查找目录的语句
1 b! w0 ]4 S$ w  v9 `% S
2 _6 F* S7 Q- [7 ^
0 E% v# s  q; @. o. y- B6 RCODE:# `' O) d+ H: |% j- E5 Y. z* \

- E7 C7 ~7 D; S5 @+ g
$ h( H* e8 t) W+ |drop table tmp;
! A0 e0 M& V$ w# K( ]! a& v# G1 Ncreate table tmp
$ B' S3 v7 o* ](
' @6 O) {) ?* \; s[id] [int] IDENTITY (1,1) NOT NULL,
7 k4 H3 j8 }( ]. d4 Y[name] [nvarchar] (300) NOT NULL," E0 m! ?- C# r5 G
[depth] [int] NOT NULL: ?: e- M, R- l7 I  d
);
0 q  F8 R2 u0 \. h2 Z
& B* q  ?7 j& Q: T2 B; U  ^declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)# W  B; p& V! I: p# N& d! |
set @root='f:\usr\' -- Start root  ~, j+ j3 t' A- z$ {/ n! T
set @name='donggeer' -- directory to find
4 p* \6 }1 G% J: w; n" h! e7 Minsert into tmp exec master..xp_dirtree @root,0,0( p1 n6 T! x0 h. N) ^, S
set @id=(select top 1 id from tmp where name=@name) ( {" s8 V7 `" y) b) y% g! n
set @depth=(select top 1 depth from tmp where name=@name) % F) D' i7 ^4 L! }, Z) r/ {% P$ w
while @depth<>1 ; I) X9 E9 @2 L6 k0 G+ V( g- R
begin ; i( Q0 L8 `5 L- r
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
. N% _6 l9 r6 V' F: F8 vset @depth=(select depth from tmp where id=@id)
4 a, p8 v, @' f; {set @name=(select name from tmp where id=@id)+'\'+@name
; A" j, Z: S' k6 w0 iend update tmp set name=@root+@name where id=1
& ?2 O* r8 P" @9 K% U3 xselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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