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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句6 B1 E: E" ]2 l# G- s. y5 X: }

% m2 j+ Z& o! p" _" lCODE:6 _# S. M( v3 u5 m
9 q/ D+ d8 U) E' L5 ]
drop table tmp;
  }: z9 t+ T7 b; r4 ?7 G1 ^create table tmp
9 ?3 N# q& w5 ]5 l( J- M) y/ F(
. ~! c' P. P6 Z- i. g& }" H[id] [int] IDENTITY (1,1) NOT NULL,
: d" R" p8 G5 [1 s) R& K" O$ T[name] [nvarchar] (300) NOT NULL,7 W' {% y. K- R3 a5 e
[depth] [int] NOT NULL,
4 H2 w/ z8 s/ N$ g, A* L% a[isfile] [nvarchar] (50) NULL6 L% {# w- F8 N, ]" C! p- E0 M0 ?# m
);
9 C- r) v! _* n, G9 T& L. {  V* ~( _2 K' _6 Q
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)8 J3 [& k) g' R6 O" `
set @root='f:\usr\' -- Start root6 k+ ^9 r5 g4 m6 M! E- g) j
set @name='cmd.exe'   -- Find file
' p' G/ i& H( O/ binsert into tmp exec master..xp_dirtree @root,0,1--
8 n! v! Q% h4 s5 t! V7 Q; Y* Oset @id=(select top 1 id from tmp where isfile=1 and name=@name) ) E. s) {2 B' ]- e
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
0 k, @1 b0 x. @( jwhile @depth<>1
* _$ {" B5 L: ]begin
$ t: n: U' l  I" s" _$ A8 yset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
: u0 ^! \' Z! e# qset @depth=(select depth from tmp where id=@id)
+ w1 N6 B0 l% U- \  Rset @name=(select name from tmp where id=@id)+'\'+@name- _" s9 v$ s9 C: `, m
end8 o6 w  f. \+ L) `/ d; m
update tmp set name=@root+@name where id=12 P9 M0 }" q7 `/ u$ Y/ x
select name from tmp where id=14 _! u% W2 A' ~$ T% Q1 z

! Y$ I* _, |( K( j5 ?查找目录的语句
- k. t4 {0 t! D- z" B9 I; b* R
, `# E: M- j) Q( b' q- g6 u6 V6 n: G0 j. X9 c
CODE:& D: O2 j& i; _! p

& C1 E2 u. U- ?+ @9 o9 h8 Z, ^9 n9 ]6 O9 X$ P. d4 g
drop table tmp;
" V" z. z& C" y2 D: ?( x9 A) H3 H2 }create table tmp
& k8 C5 }& j' W. p$ ^7 q3 D" ~4 E(
( j0 M- P1 @) K  |+ n2 s* q$ f[id] [int] IDENTITY (1,1) NOT NULL,
- x+ w. ~$ J+ e( Z0 A8 [& N$ `[name] [nvarchar] (300) NOT NULL,1 _% Y3 w: Q7 o, S3 m, x8 a
[depth] [int] NOT NULL
" j0 m2 ^4 d3 c);
6 B1 ?( U, i, ~* C, ?
1 B. |( _7 D( ?% ?; C  h' Hdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
( V1 }/ S0 C/ K/ n) N8 ]4 L# iset @root='f:\usr\' -- Start root
7 K6 m& v) y7 o, y* f0 A7 {+ c6 d$ Cset @name='donggeer' -- directory to find
9 L' ]8 _3 S3 J9 J% \. Iinsert into tmp exec master..xp_dirtree @root,0,0
9 z" A1 J! c- k/ ]9 p2 eset @id=(select top 1 id from tmp where name=@name) 6 U6 K4 p' g# S9 q7 S
set @depth=(select top 1 depth from tmp where name=@name) 9 Z" M9 @& O- E6 e! b
while @depth<>1
1 f8 X8 O, {) Q* F6 R) Dbegin $ I6 P1 [" ?0 D0 \
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)$ e; d9 K! k$ K' ~* c
set @depth=(select depth from tmp where id=@id) $ S1 x; L5 z& u
set @name=(select name from tmp where id=@id)+'\'+@name
% w" a( \  J! r, wend update tmp set name=@root+@name where id=1
& K% G) d- P+ q5 h0 `  Z7 {select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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