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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
, S& d5 i3 W' _9 K% b% s. ?% L
3 c% S' X7 o2 l6 `$ ]CODE:
- J. D/ w5 [; F, q, j$ c* F7 A# W
drop table tmp;: M% R  U* M% @" F: M* V: }
create table tmp
/ q* j( k5 @+ A, u5 }(1 O  i4 _' {8 a! w% E7 F
[id] [int] IDENTITY (1,1) NOT NULL,
' n2 R" U! S7 W% c& T# N+ ^! o[name] [nvarchar] (300) NOT NULL,
( `2 Y: b% ?& g[depth] [int] NOT NULL,
2 ?( t; U0 m4 e7 e- \% M! G[isfile] [nvarchar] (50) NULL
/ A. t" \8 b6 u  ~& Y8 n9 x0 A);# T* x6 q4 j( X+ q
5 g% u# E0 c& H1 h  k4 F& D  ?. F: B
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
# M6 W. M0 ]9 `% X+ X0 y; v$ P2 Lset @root='f:\usr\' -- Start root- {$ V5 T# o( N2 D7 w" r
set @name='cmd.exe'   -- Find file* W- o+ B* C; P  S. Y
insert into tmp exec master..xp_dirtree @root,0,1--
+ R( E2 n9 H4 Q  d& M/ Sset @id=(select top 1 id from tmp where isfile=1 and name=@name)
3 [: c- Y) L. G9 Q' \3 S! Mset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)5 P$ r( h0 H* \( F- B8 B; l
while @depth<>1 ; ]+ m9 q5 [8 d
begin 0 m1 I7 ]- [7 }# c* Q0 y
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
7 Q" j! }- k4 M/ p0 Gset @depth=(select depth from tmp where id=@id) 9 ]: [' @# c9 P
set @name=(select name from tmp where id=@id)+'\'+@name9 U0 g- Y( S& l7 q- `+ Z$ Y* G1 h
end
5 |. {; P! f! R, G% Lupdate tmp set name=@root+@name where id=19 H1 L* E4 |$ L. i# Q* Y
select name from tmp where id=1( B6 D& g" {0 b- Y; s# Q

7 k3 R8 f% r: j; B查找目录的语句/ l. v* a1 R: K
# }$ e2 x; X9 X6 G& Q

% U- f- a- d( L& a6 UCODE:
) G4 z$ W; l1 p6 b) W. T- n
0 i) ~& j8 N4 {5 J! Y9 W+ r" Y# R2 K$ p0 l/ k; T
drop table tmp;
" [2 Z, G1 w! T) ccreate table tmp
5 E: L! g% w8 g2 C) d3 n0 [) |(
7 `: n/ `: i1 y, U; I. Q" I3 x- @[id] [int] IDENTITY (1,1) NOT NULL,3 |( v( s' u/ {
[name] [nvarchar] (300) NOT NULL,
. D* j9 k; x/ P# G; v[depth] [int] NOT NULL' v+ L/ g, G* c1 t( k
);
8 c* @" V% y  Q  j7 ?+ i: o- \  V4 T1 e& J7 c9 G5 `
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300). ~' {( \. @' H# w- _/ A5 z4 t# }
set @root='f:\usr\' -- Start root5 M& ~& P, W/ L; c
set @name='donggeer' -- directory to find
0 X. a2 {8 A% \8 N" n9 ^4 Y; pinsert into tmp exec master..xp_dirtree @root,0,0
' s7 I# Q' V" \5 |6 s; c4 t3 Oset @id=(select top 1 id from tmp where name=@name) ; _: z- X9 e7 B: `& {/ G6 B
set @depth=(select top 1 depth from tmp where name=@name) # Y( _: s) o" U+ K3 M0 B, |6 y
while @depth<>1
4 V2 f/ q# P& {' e; }" i" Fbegin : ?6 Z1 q, \% _' T; Q9 D- s
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
' h/ ?2 J, _- uset @depth=(select depth from tmp where id=@id) 0 D, A0 ]0 W4 `9 j) i3 x
set @name=(select name from tmp where id=@id)+'\'+@name
0 J+ j  a" b! g6 K2 L4 T8 Vend update tmp set name=@root+@name where id=1
# f7 n. r# e: `+ }: N+ E: Uselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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