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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
查找文件的语句
! a9 |# w- a" L7 Q$ ]" v
, M1 e$ O) A3 \) SCODE:/ e7 Q1 Z" \4 J- C

6 S8 X# @+ S, Q; i) R: k3 Idrop table tmp;
% {/ B( K" P) ^4 V( E  _; xcreate table tmp
% f& Z9 r( r! m: Y() o# @2 p! {$ M2 n/ O7 m1 m
[id] [int] IDENTITY (1,1) NOT NULL,6 m) n. J2 l* o! c) p
[name] [nvarchar] (300) NOT NULL,
- _& M4 P; K1 D; j[depth] [int] NOT NULL,
6 Y; v- z! U% F0 [+ G$ c[isfile] [nvarchar] (50) NULL% [( l" H2 B& [
);- S7 x8 _- ?0 i! M1 I; P4 t. R

: s3 _+ v! C6 l( b+ G9 fdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)& f& j! h  e. r4 H7 l+ C
set @root='f:\usr\' -- Start root
7 T! c) w6 c/ f7 Q' J( fset @name='cmd.exe'   -- Find file
6 J  I% J& u: i3 k' Jinsert into tmp exec master..xp_dirtree @root,0,1--
; s/ y( Y* ]2 M& Z1 y: ~1 N4 Kset @id=(select top 1 id from tmp where isfile=1 and name=@name) 9 X$ |2 {1 S& B' n: G% f
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)* Q6 }7 {3 p4 q1 ~) i
while @depth<>1
+ K* W7 q" `+ }' c2 D7 u. y* zbegin
$ J% }8 m5 Q$ k% K9 N$ S0 vset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
5 J; W4 G  a! J- l/ |( ]6 A+ \3 Yset @depth=(select depth from tmp where id=@id) 4 O+ Z" m; F9 R8 v4 {) c' Z1 {
set @name=(select name from tmp where id=@id)+'\'+@name
9 i6 `, }4 \% v+ x  Dend  c. C6 l5 x5 L- g% |
update tmp set name=@root+@name where id=1" I8 O( B2 U# ?5 [) G5 c
select name from tmp where id=1
3 a$ b! ?3 W' c1 ~/ U, J: r' l9 z2 \8 t6 @! Y0 M+ d1 q
查找目录的语句
+ t) {& p9 e1 j, j$ X9 S9 ?9 t* `% M: G6 q

( U% ]6 l: k$ _% NCODE:
0 d3 E. H+ ]4 U* o  y/ c( V$ o  S8 _: J* G7 ?
% r1 d7 v+ [) k, P  Z
drop table tmp;, n1 t- s. V3 i" S
create table tmp
. o2 }2 \" I% [! G" ^# |' |(
, ?5 Q+ b' s( f: {: g+ |8 B$ [# t[id] [int] IDENTITY (1,1) NOT NULL,+ e& w+ l7 N3 i
[name] [nvarchar] (300) NOT NULL,0 S) l6 j# ?* }2 o1 [) }9 s
[depth] [int] NOT NULL
, A5 N$ v' S) Q6 Z);
) ?- K0 S( ?2 `6 Q; D. e% f( T8 m8 [$ F! Z2 L* |& o( N" g* L
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
& n# p6 z( }1 y$ Wset @root='f:\usr\' -- Start root
7 l  ?2 v) ^, x' X( k- P: iset @name='donggeer' -- directory to find
" V. k& r* g$ Y8 Zinsert into tmp exec master..xp_dirtree @root,0,0) ]2 |- C' Y( w$ @7 {% L1 l
set @id=(select top 1 id from tmp where name=@name)
7 G: K+ R% u4 N* K: \) z9 [set @depth=(select top 1 depth from tmp where name=@name)
5 H3 X! K1 r7 rwhile @depth<>1
% }2 E$ W# r6 ]. l/ E$ }begin
. c3 ]2 {- D1 K* W5 Pset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
7 L- ~: ^# t5 g. `set @depth=(select depth from tmp where id=@id) " J+ T$ g3 J2 J& V! s8 [8 f
set @name=(select name from tmp where id=@id)+'\'+@name * ^  l# F6 [; k; {$ n
end update tmp set name=@root+@name where id=1$ n$ n4 [+ w, u4 v
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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