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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
0 B! q: v' \3 e8 P5 E6 J+ k0 z7 y0 b7 R  K6 d; B' ~1 R6 h4 b
CODE:
/ w' ~" ]6 O$ f3 _) K! J! [' z5 w' n/ w/ ~$ h# o
drop table tmp;. G2 v" B9 _/ s
create table tmp$ H. f+ x$ S' \( V
(3 R# f7 f5 P0 i
[id] [int] IDENTITY (1,1) NOT NULL,' N3 [$ D2 S; ]& P
[name] [nvarchar] (300) NOT NULL,
3 Q: G9 ?0 ?* S+ y. O7 p, D[depth] [int] NOT NULL,
9 F* U! [( @( }; ~% x9 `3 D4 b[isfile] [nvarchar] (50) NULL# L+ j/ [/ e2 _8 v, i7 n+ W  y
);9 V- [. t0 g1 j: `3 o7 R
/ l1 T* N+ w" s
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)) L- U0 Y, {; N6 q
set @root='f:\usr\' -- Start root
, t6 e# ]! Y' S: m) i7 }: s- Aset @name='cmd.exe'   -- Find file
% x' C* _% Z- K. y# uinsert into tmp exec master..xp_dirtree @root,0,1--
" p+ e$ U+ r! g) qset @id=(select top 1 id from tmp where isfile=1 and name=@name)
3 `: x7 @, ^$ x, n8 H( Lset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)# e& L$ }9 q8 d$ O
while @depth<>1
" D6 |0 `+ W6 }2 M8 ~3 ybegin
1 m! I. J% m; Qset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 7 V5 b' i; r) K: Z9 b4 y
set @depth=(select depth from tmp where id=@id) $ |, ?) N2 ~/ X/ B
set @name=(select name from tmp where id=@id)+'\'+@name$ U) B( B- G3 J) S7 U. K, S# x
end  H$ v- s/ U5 A+ j
update tmp set name=@root+@name where id=14 X. U& S* m. d. R
select name from tmp where id=1$ t" f4 s4 k- F
. B% K) [2 r9 F; ~
查找目录的语句. R1 E: ^. W& ~2 ^( I' L5 v7 L
; l- H! i, k  l

& @( @  |4 l1 bCODE:
( C- H/ ]! j% D0 t+ K' F6 k% Q! h! v5 l" N, W% x3 Q# c

6 f) ]3 S( ^' V/ A, U) _' @drop table tmp;
  L8 F3 v6 w) y% [create table tmp) Y6 t8 H# ^2 R% n* J  h
(
8 s, ^, T+ E( N; ?- Z% c[id] [int] IDENTITY (1,1) NOT NULL,& d3 N' K4 f# w* `, ^7 B
[name] [nvarchar] (300) NOT NULL,1 E# D# e$ D. Y( v  J' ~
[depth] [int] NOT NULL
! }- ?0 L( ?7 L& A);
; [" s% i) k! A! n! F9 ?* P0 B) A, S& ~8 q
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
, j, ~, T+ ?4 ?0 ^) Fset @root='f:\usr\' -- Start root
( K- i0 [( }" a/ Mset @name='donggeer' -- directory to find" G: A# p6 R; ^3 A& t: R8 n
insert into tmp exec master..xp_dirtree @root,0,0- n1 a; ?/ ]  }
set @id=(select top 1 id from tmp where name=@name)
0 |# _( I6 F# e& z  Yset @depth=(select top 1 depth from tmp where name=@name)
. E7 _4 x8 ?2 r" {4 P* ^% jwhile @depth<>1
7 P6 m1 y2 }( `) qbegin 3 t# W/ B+ E. N9 w/ ~$ @
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
. T, o: `/ m' [3 c. R, rset @depth=(select depth from tmp where id=@id)
, u! q+ L' g7 V  X1 _; f; Iset @name=(select name from tmp where id=@id)+'\'+@name ' o+ {: n- J7 X& |0 a
end update tmp set name=@root+@name where id=1
$ o! R  p; |; |& lselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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