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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句6 g& c  Y2 m% L. m* o2 g

, T: a3 {3 ?2 I. J7 |+ i1 K; g2 XCODE:
8 |0 G3 _+ o& m9 M% D
3 P! b7 }% F' _" Z" Kdrop table tmp;8 _& X; ?8 ?* ^* i
create table tmp
9 ^. d# ?% f; S) u(
  C4 H) z3 M! W4 K+ o! q; h! @7 ?[id] [int] IDENTITY (1,1) NOT NULL,' O  X' m9 u" r4 k4 |' m' c
[name] [nvarchar] (300) NOT NULL,/ T1 b, ]4 y* |
[depth] [int] NOT NULL,+ l- S2 F3 k0 S; Q- Q" x; u# ~
[isfile] [nvarchar] (50) NULL9 w1 d' l; |9 W9 a1 d) _) }
);+ S# f& {* ^3 z& M) t
! B" |( o8 X8 }7 r( H
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)9 F2 |" n) S% v; j( e
set @root='f:\usr\' -- Start root/ S6 J7 r$ M- S- p
set @name='cmd.exe'   -- Find file: Q+ e& X2 L# O1 r- O
insert into tmp exec master..xp_dirtree @root,0,1--' A" D" `. h& N, d* _/ c
set @id=(select top 1 id from tmp where isfile=1 and name=@name) ; {  O+ J9 _( f8 U; x0 W" i
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)7 F# f5 z; C: {* u; \) I; `
while @depth<>1 ( N1 d3 U5 U6 G! @: y) Q
begin   H+ |2 g( T8 j8 b
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
) J7 `  @8 c* j( R3 y) S7 I: p' [set @depth=(select depth from tmp where id=@id) ) K5 I/ \* {9 C7 ?# B
set @name=(select name from tmp where id=@id)+'\'+@name
' U3 a, m# l1 t+ Iend- {9 m' ?6 w8 ^1 P/ C- Z
update tmp set name=@root+@name where id=1* ~$ @! M/ {. `. K
select name from tmp where id=1
, O/ B# h, v+ ?& i8 d7 M' t+ Z& S- `$ D3 W# k
查找目录的语句' o7 O8 F- d5 O' t% ^5 [1 P

0 ]1 a3 |& A+ C# G
) t7 X2 H7 J3 ?+ }CODE:* E% e% A* D  q3 H6 ?

2 I% @3 O8 c0 Q6 B: P" w9 ]1 Z. v0 k* V3 j* p0 t
drop table tmp;
: n) L6 @7 f5 H! h  q9 W& Xcreate table tmp4 D; A4 h$ t1 C2 F7 s
(4 P' ^0 W5 y: h+ N# d+ ~. }
[id] [int] IDENTITY (1,1) NOT NULL,
2 s( g1 }3 ]7 _. b9 Y' h[name] [nvarchar] (300) NOT NULL,
" U' y: P3 w' I: t! b1 H: a1 G# E. P% W. ~[depth] [int] NOT NULL; f# G( R" ^  Q( S% r% x: {1 I
);
6 J' i  K/ S: z) d
, G) O6 |/ W& C) N' fdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
5 F7 g& p4 s. [  q) ~set @root='f:\usr\' -- Start root
/ e& A: B& a; o; [! }set @name='donggeer' -- directory to find+ l' A' E! W% G0 s- }, f' f  _
insert into tmp exec master..xp_dirtree @root,0,07 `, K* x4 k; d; c" k% ]  A
set @id=(select top 1 id from tmp where name=@name)
( ^1 }) s1 B8 I2 z4 ?3 ]  Iset @depth=(select top 1 depth from tmp where name=@name) ! k- _0 s) o# U2 ~, I! [
while @depth<>1
5 Q9 a. l" H1 ], Pbegin 8 ^6 Z3 p; Z+ P8 J: Z& g( e
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
, B- W; ]! b! p6 `set @depth=(select depth from tmp where id=@id) * a& b: |; u8 O7 O# T, i' y# q: |/ O" h
set @name=(select name from tmp where id=@id)+'\'+@name   B6 }1 I0 O2 V" D1 _- H
end update tmp set name=@root+@name where id=1
3 P4 M& N4 }5 {5 G# Jselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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