找回密码
 立即注册
查看: 2375|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句9 q! v" d6 S+ X9 O- s4 S6 c( O
* z* m" c8 Y! |2 Z1 c" c  [
CODE:
1 r* R# q: d, r! P5 s1 c8 P
+ k/ J( a7 G$ Idrop table tmp;
% M% T6 h8 h- r# i7 U8 Pcreate table tmp
3 n5 E: \- Y* g7 E6 g+ c+ h* z* s2 [(& [0 `& j" h' o* H
[id] [int] IDENTITY (1,1) NOT NULL,# G# W9 T2 Y" h4 n% Y
[name] [nvarchar] (300) NOT NULL,
& Z$ ]4 n; H) _3 v5 C' c7 w[depth] [int] NOT NULL,4 o3 L. g* z9 K5 M( h6 Q" q
[isfile] [nvarchar] (50) NULL1 O- ?& S, o2 S0 O6 s5 J5 t- v
);
( e7 }6 d, {$ m6 X: [+ z, Y# ~9 L  r
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
0 b' f6 z, p0 P- a5 @set @root='f:\usr\' -- Start root
' C" D8 m) k% P: C4 tset @name='cmd.exe'   -- Find file
) P5 L7 h) l, F3 d/ Linsert into tmp exec master..xp_dirtree @root,0,1--; {( S! G8 U2 D9 W8 F5 E
set @id=(select top 1 id from tmp where isfile=1 and name=@name) / ]" i: T) Q+ o7 l# w* B
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name), H& X3 X! b. F
while @depth<>1
' k- f5 k! T3 R" j4 a( O1 ?9 lbegin
# Y6 Z4 @* C8 ^( p) Qset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
- j8 z6 ~! v5 H) M+ B5 ]& V! F6 pset @depth=(select depth from tmp where id=@id)
4 R* }9 ]- N  _8 Sset @name=(select name from tmp where id=@id)+'\'+@name
9 C6 b( T( |6 y0 T+ V& Yend
. L3 U+ V/ y1 ?" wupdate tmp set name=@root+@name where id=1: d! @' m% W( H6 L& _
select name from tmp where id=1
6 C4 ]$ J  r( v* @1 J7 T1 D
1 _/ j3 |) D9 S% `" \查找目录的语句  K- z8 j! C: p
6 t4 T9 S. k/ S

# A; Z4 w' m. q8 {CODE:
% t" _, ?4 D& q1 A: ^3 m# X) L

! x: s5 S- G( Q1 G# a+ _drop table tmp;& \0 C8 E7 ~# ?6 n5 f/ k& ]3 f1 g
create table tmp- m7 E/ X3 y1 f/ o% Z5 ^
(( R, a1 t0 T' q) x2 o; u8 ?; {6 Q3 A! j
[id] [int] IDENTITY (1,1) NOT NULL,
8 V# [) a' K' f; U[name] [nvarchar] (300) NOT NULL,. l/ F: }0 ?' x+ v6 S2 m
[depth] [int] NOT NULL" f, T6 x( ]) O, U
);
" Y- f3 S1 m# _) p  ]. [+ V
4 Y; B% D  C7 i. R4 g9 @, }" [7 {declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
, H& X& b* G& [7 s/ U7 Y+ qset @root='f:\usr\' -- Start root% B5 W+ K/ H! }* P' T' Q$ z
set @name='donggeer' -- directory to find
1 w# D- x; a: rinsert into tmp exec master..xp_dirtree @root,0,0
' k* k0 O9 X8 v+ N2 c0 z' q5 b3 k. W; cset @id=(select top 1 id from tmp where name=@name)
; H9 A, D! d6 w8 f) o* d5 B7 iset @depth=(select top 1 depth from tmp where name=@name) , i+ G# a, V# k5 f& q- ~  X
while @depth<>1
- S- O' H" m# S8 cbegin
, ^; A* y8 [- a( Q9 zset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
" Z, t) k+ i- f. `! `) k% Oset @depth=(select depth from tmp where id=@id)
1 l. U6 u: a  F! o- S( ?set @name=(select name from tmp where id=@id)+'\'+@name / F0 m1 F: S2 g2 N1 A; o) O
end update tmp set name=@root+@name where id=1
6 v  h- P. f; }" J3 k6 x* y2 P" bselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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