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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句& \( `9 m  m0 n+ a- j5 ?, H6 m/ @1 S6 O

+ ?: L6 S5 H* S; y5 v/ c2 T: n3 ?7 [CODE:
" y: x3 D" s4 `0 B, h9 f- F
% k% G( v6 l9 H0 F) Wdrop table tmp;7 m. M5 [8 I8 p1 {* N
create table tmp! R5 s/ t3 U/ z" `- p3 i9 r( J4 R
(
) \- M7 |1 v# P7 n1 F3 \, a[id] [int] IDENTITY (1,1) NOT NULL,& ^4 O) E8 y7 P5 O
[name] [nvarchar] (300) NOT NULL,
) ]6 Q1 y, s5 n0 F[depth] [int] NOT NULL,- m; R' s2 ?: E& ?% x, R+ b# T
[isfile] [nvarchar] (50) NULL9 s# C  r, ?! e* y' ]
);, H7 j  J" ^' G1 `3 f) r5 ~

% X* J2 b- e- k2 |* ^3 H8 Cdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
+ K3 I4 I# h8 U/ K7 p8 aset @root='f:\usr\' -- Start root
; _" |( {: e/ f' Aset @name='cmd.exe'   -- Find file
) E4 k" q( \4 n3 s% l3 }$ o% iinsert into tmp exec master..xp_dirtree @root,0,1--# S. l) z4 _  O6 j' f
set @id=(select top 1 id from tmp where isfile=1 and name=@name) $ s; E3 a/ r# P4 ~) W
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
- H  X! T) ~6 V3 o! u. H0 rwhile @depth<>1 2 C; i/ p; [8 }( o! f7 B4 C
begin
$ l0 D5 }$ [, f8 [set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) " q) p# Z- g* [! P
set @depth=(select depth from tmp where id=@id) * I6 l/ l) R8 E6 B0 B7 S$ v  X# }
set @name=(select name from tmp where id=@id)+'\'+@name
% T8 A: ?* ]/ i8 |/ Mend
  t2 z5 V5 _, _, Tupdate tmp set name=@root+@name where id=1
* \" y9 n8 t. r* rselect name from tmp where id=13 L. f1 p+ G+ O2 |
* _9 Z" A/ l9 Q+ t9 Y+ i% G, U
查找目录的语句2 a: r3 s4 l# D
$ o8 b; B% _, e
6 p8 |2 L' @4 q6 x1 t, |' w0 F; j
CODE:% \( l) w5 P$ R' D
# R/ \, I- d! ~& \8 z$ v

- ]3 B+ I& L' t* {! `' c) ^1 Vdrop table tmp;
4 t4 i- G- ]* U) v* L4 ocreate table tmp
9 v3 X* i( C; |4 I  ?3 P" a, O(( Q# ?. H" C2 {+ d4 u- r( u
[id] [int] IDENTITY (1,1) NOT NULL,% u' W- J, v# d0 Z$ N" L8 G; S
[name] [nvarchar] (300) NOT NULL,
# _$ Z: I0 S3 W3 f- B8 F[depth] [int] NOT NULL
, y/ ~+ j# y5 p- B3 b3 ?2 O$ a);& M( c' `3 }1 g1 ^8 F) J
; ~* @' A% L( h; l  t3 y
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)% l# h$ H  |/ ?( L% N
set @root='f:\usr\' -- Start root* n4 m+ r; D! S' W4 u' i" F
set @name='donggeer' -- directory to find6 a1 A% {; ?+ r" I  f; A: f6 t5 P
insert into tmp exec master..xp_dirtree @root,0,0( u4 B$ E+ N" [  V4 n# S! W1 g
set @id=(select top 1 id from tmp where name=@name)
, h+ B; m% ^3 D( d1 ]set @depth=(select top 1 depth from tmp where name=@name) , _7 n  a  _" F& R8 h. t, B0 i
while @depth<>1 ) W$ z7 u: {3 ]) a6 t6 Q) [
begin
" v9 G  f. \3 t9 o+ @set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)- c" z% r' |3 ^+ B0 T
set @depth=(select depth from tmp where id=@id) 9 X2 Y- e& S4 Q  L
set @name=(select name from tmp where id=@id)+'\'+@name
: E1 `/ c' X' N4 t0 o- Q! Y) @end update tmp set name=@root+@name where id=1
" x6 w+ T# f3 g/ v0 n; k- Lselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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