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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
- c% g, t* w6 p6 Z# L
" b0 G3 x6 h& WCODE:
, V# }# P# N8 K& C# G% T% T7 W5 u" ]6 g2 V
drop table tmp;
6 u$ h# i# V! E  l$ [& @4 K# Y* P* screate table tmp9 }2 x" w8 c5 e
(5 m) ?2 C% q; D# b) P
[id] [int] IDENTITY (1,1) NOT NULL,
2 G, G  B9 Y2 c) Q- _. w9 B/ ~1 ~[name] [nvarchar] (300) NOT NULL,
  D. I: E5 q$ c  t" G[depth] [int] NOT NULL,0 {7 b/ X' f. W  B( P; O* F$ l3 ?
[isfile] [nvarchar] (50) NULL
) H; H7 ?( x1 ?; m$ G% c+ g& @7 z);) q9 N" b2 Q& B) D, U

" a6 \" E8 q0 v) i- r9 [+ Vdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
3 k3 c. V% R! w# l  Uset @root='f:\usr\' -- Start root' L/ u$ E9 u/ {5 d0 x
set @name='cmd.exe'   -- Find file6 R- @- v$ a# r* Y6 c5 c
insert into tmp exec master..xp_dirtree @root,0,1--5 ]9 X- B' n5 r* y4 q6 [
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
5 _; R# ]- `4 P/ [& F1 Fset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)4 v+ g) P6 ^+ g% Z8 ?/ e/ u
while @depth<>1 ; G# o" `4 g# `2 T5 g
begin
9 T; q% B! z) b5 {4 }set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
/ B7 ^4 v* l! B4 Fset @depth=(select depth from tmp where id=@id)
0 u& T3 ?! A  q! M! U) G! x; Nset @name=(select name from tmp where id=@id)+'\'+@name
5 ~7 @5 u8 H9 h# _2 @. H% q* Dend7 n; C5 A$ M  T. ~3 K5 c
update tmp set name=@root+@name where id=1* D0 D; p7 V* I1 t* K: L7 k
select name from tmp where id=16 z/ L; K) W. Y% h( m" Y' O
& \) a* D1 J) y3 l( H! t- Z% k
查找目录的语句* m0 @5 x, R9 w1 z6 s7 _5 u

) R& u% m3 _8 a8 a, i/ f1 ?7 @. e4 \  {; X0 Z
CODE:1 V% r' X" [3 d0 x+ Q

7 O2 x; c) t9 a1 m8 x# C
0 h( o5 |5 D. t7 Y$ u+ vdrop table tmp;
$ S1 j7 Q6 `& z, B: F. r6 ncreate table tmp
' T1 p  v% `$ [# d3 }(2 O; X2 a/ D- b  i7 }/ M8 R5 N
[id] [int] IDENTITY (1,1) NOT NULL,
* J( w% \( J8 [, E3 W# R[name] [nvarchar] (300) NOT NULL,
8 F! j9 Q+ M3 G5 \* Z% \- E[depth] [int] NOT NULL: S: v% C: H0 }4 v) l8 b
);: o  u  N- W' N

% ]! {- d* w! S* }' xdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
5 B, B9 b' W- a( iset @root='f:\usr\' -- Start root- J  v. m( A, T4 s/ x
set @name='donggeer' -- directory to find
0 |4 r6 v0 C  B0 U' T3 D/ G6 einsert into tmp exec master..xp_dirtree @root,0,0
# e2 B) l' R9 T" nset @id=(select top 1 id from tmp where name=@name)
- h1 u: v- |3 Y5 I" H: L+ k- s+ Dset @depth=(select top 1 depth from tmp where name=@name)
# P0 ?6 W$ f2 Y, kwhile @depth<>1 . q0 Q4 b8 T4 Q& b
begin ( A# D6 O1 M8 L* T# c
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
& \6 ~% H: X/ Y9 Gset @depth=(select depth from tmp where id=@id)
% h( q" Z- g2 N8 J+ r0 ~set @name=(select name from tmp where id=@id)+'\'+@name
) C$ C" U3 |. X# \$ S* Y, m0 @end update tmp set name=@root+@name where id=1
( K' D" w9 |2 f6 N6 eselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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