查找文件的语句
/ z+ u/ |8 x: _& P' ~/ \% T, X7 }, W' A- _' w! F$ z+ l
CODE:) w. U/ Z! K; x! S- a
5 p1 r) y1 w6 T6 E! ]2 E' p' ?drop table tmp;
; E$ o/ I% O: o3 z: D* ucreate table tmp
* W f3 z; |, f(
# R# z. G$ J. b* [- F% Q$ u' Z[id] [int] IDENTITY (1,1) NOT NULL,3 t8 ^% b: ]4 c
[name] [nvarchar] (300) NOT NULL,
5 Y) C, U$ r# E( F5 u; J5 N/ o[depth] [int] NOT NULL,
, Y1 L( r9 P4 Z[isfile] [nvarchar] (50) NULL- Q. ^$ `1 {& B
);
H. {% u( I2 z+ e! N% u
* s% {, H6 w1 v4 |$ k$ J/ N/ Y2 Adeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)6 k* ?: I B E" J& A
set @root='f:\usr\' -- Start root1 n0 H# k& U: G+ k. c b
set @name='cmd.exe' -- Find file% ]$ o( W x N5 {+ }% `/ i
insert into tmp exec master..xp_dirtree @root,0,1--
k% C& {# J+ k3 q' s! K3 q: jset @id=(select top 1 id from tmp where isfile=1 and name=@name) * {( Z3 Z( s% ?0 Y+ P5 ^; J0 Z: s& d
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)6 h9 }+ }: A! E% C: ]" M
while @depth<>1 + k0 P+ y: ^; T4 ?: z7 P8 }& S
begin ; l" b# M! N1 U. O
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ; n% b- ~2 u# h+ s/ Q
set @depth=(select depth from tmp where id=@id) / |7 e& e. G2 J
set @name=(select name from tmp where id=@id)+'\'+@name
9 Y7 }- |9 B0 n+ A9 `, k% _/ @end7 k: J( X7 E% |: L( N+ T
update tmp set name=@root+@name where id=1
3 H! R5 X1 D# n- u) @; P& i$ W9 nselect name from tmp where id=11 F% W; g5 R, E- e
# ?& D5 w( M4 M& }) G: Y: Q
查找目录的语句: H. \5 u7 v3 ^. Q* X
6 t! c6 u ]; d- F+ _
0 |' j) S3 A) H9 C' cCODE:) D- v, `. b! K7 n3 z
5 u7 E M M: p# s2 I
, A6 o$ U! m2 N+ J3 D. j6 W7 H
drop table tmp;
0 o: j& ?5 |/ E5 `7 U& t4 Jcreate table tmp
2 r# S/ p/ w, Z7 z(; m: G- l# J6 P" D
[id] [int] IDENTITY (1,1) NOT NULL,
5 S% l( [6 C0 T. E5 o[name] [nvarchar] (300) NOT NULL,( t7 ~% `/ q4 j- K
[depth] [int] NOT NULL" Q: k5 Q: _4 @
);, y' O) p) K2 V$ ]
% y @8 z* r3 l) y3 [, s5 ]7 d
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
+ B, r5 ?& A* \( P1 x# wset @root='f:\usr\' -- Start root$ v4 u& h# U8 ]
set @name='donggeer' -- directory to find
: B8 ^" N( `# C, z3 p) Vinsert into tmp exec master..xp_dirtree @root,0,0! Z2 ?2 v4 @3 m/ H
set @id=(select top 1 id from tmp where name=@name) 2 K+ @) b- A' J# e
set @depth=(select top 1 depth from tmp where name=@name)
. {- g$ N, @$ ~0 r6 p: }9 `while @depth<>1
9 y2 V7 o$ T9 A X rbegin 0 O& l: k( r# w4 k
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)% U) \, ]5 P9 ~8 q$ d
set @depth=(select depth from tmp where id=@id) 6 e4 B( y7 G( n+ U8 g! P! I
set @name=(select name from tmp where id=@id)+'\'+@name
" W) R/ U# @7 o# ~- U+ Send update tmp set name=@root+@name where id=1# N) H" N1 h3 |( j
select name from tmp where id=1 |