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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
. M  Z- N2 [' U& O/ c1 ]. P2 Z/ I7 N3 j
CODE:
- Q0 b7 j; I- Y' ^) Z) o1 s7 i2 a! h, Z2 N2 N0 ~/ s
drop table tmp;
9 z5 N( S7 X" qcreate table tmp
- O4 b" W1 ^1 R) ]- S, `) V(9 ~) U) F; G" E  u2 D9 y
[id] [int] IDENTITY (1,1) NOT NULL,+ q, p8 ~9 }+ n+ v
[name] [nvarchar] (300) NOT NULL,( T3 X2 x0 h: Z% P! Z  z
[depth] [int] NOT NULL,' A+ P( Z2 x( t9 R5 x: M/ f( F/ u3 Y
[isfile] [nvarchar] (50) NULL
- D; x1 C8 \  Q1 ], W);
# t8 }& S1 B4 b# g/ W5 M' x; s) \4 s8 \  C- R  X
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)" A  j: {5 o5 d" B: v9 y) s
set @root='f:\usr\' -- Start root, N8 Y% \+ L4 B- A8 L) e; I8 b5 g
set @name='cmd.exe'   -- Find file; f* C7 ^7 k- ], e" x- E* X' e
insert into tmp exec master..xp_dirtree @root,0,1--6 }; y  m& d0 N
set @id=(select top 1 id from tmp where isfile=1 and name=@name) & ]- u8 j( s; Y: g8 f# h
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
# v  r; f$ m; j% j: lwhile @depth<>1
1 T, R9 M: U* e6 m& _- }9 E; Cbegin * h& F7 v( c+ p& [5 i0 \
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
! P% X4 U/ D' |0 I6 ?* vset @depth=(select depth from tmp where id=@id) 1 X8 t0 `) N2 p, N9 u
set @name=(select name from tmp where id=@id)+'\'+@name: L* y4 A/ X% R& }$ j& P% ~9 V
end: i8 w; @2 K1 R1 H
update tmp set name=@root+@name where id=11 T9 t& o: W# Q4 h) K& ]
select name from tmp where id=1
5 x6 m1 ^! x+ z! `. `, n) N$ n/ z+ y& v! m& o7 j% f
查找目录的语句
* ^. }9 b- |: x! m, B+ E0 C- |
, I9 E! I0 U$ Q& @5 f/ V
  O0 _7 f9 t; k; I( Y* SCODE:
+ n3 g) p. h. @# z% I' e, }( P9 _5 M6 B) {( ]
# {$ n( L$ a' [9 G1 u9 Y
drop table tmp;& k  `- [' C6 ?6 I) h
create table tmp3 l0 J% r( q1 y( g3 L4 \& t
(" V; ?8 H- k, U$ V2 j2 E
[id] [int] IDENTITY (1,1) NOT NULL,
9 g% w: ~0 K) C[name] [nvarchar] (300) NOT NULL,
; R9 E, X" D7 T# u* E% i[depth] [int] NOT NULL' S7 H1 Y- m0 D- G6 O7 p
);& a1 x% O: J! z( O3 X' t& r/ C
5 S5 q& K; |' K3 W; o
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
9 S0 C# ^2 }) D2 u& @, tset @root='f:\usr\' -- Start root1 V% d" |0 D0 G' L2 |% M3 B" ?
set @name='donggeer' -- directory to find3 X( w- L5 s; E5 h; c% }
insert into tmp exec master..xp_dirtree @root,0,0
, W5 p" o; }) ]& H9 ?set @id=(select top 1 id from tmp where name=@name)
' g4 p; s9 d3 y+ p6 Y% H" Eset @depth=(select top 1 depth from tmp where name=@name)
' S+ C  _9 |$ _, o4 Swhile @depth<>1 8 f2 K+ S# ~1 r) w; y
begin
7 x$ ]( @* y7 @6 t' h/ ]set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc): x  `1 e- n: m$ Z9 i$ `
set @depth=(select depth from tmp where id=@id)
6 f: u3 Y0 S' q" Cset @name=(select name from tmp where id=@id)+'\'+@name
4 p- g7 f- [* v8 [7 D0 O! Uend update tmp set name=@root+@name where id=1+ u0 b3 d7 l2 H7 [0 c9 T/ ^
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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