查找文件的语句+ S1 S1 q- n* M( q
$ K% P: ?4 D( q6 U: q* E! l2 [CODE:2 J. E$ {3 j4 M5 a6 X5 V. W
' V5 Y8 G Y" X, M( ?
drop table tmp;9 S. u% F1 t6 O6 s# p! n% L. _! @4 h" B
create table tmp
$ w) X4 h$ A7 R% s8 j: o(
4 b& ~) p" o/ K' J- z, S6 c6 r[id] [int] IDENTITY (1,1) NOT NULL,( d `# W2 @) ]5 p" l. C/ p4 J
[name] [nvarchar] (300) NOT NULL,( t1 {1 t1 f; @( X1 ~" p; p
[depth] [int] NOT NULL,
B1 p8 T+ A4 L) m& \2 r[isfile] [nvarchar] (50) NULL
) {* ~/ w2 I& ?, `# W- w);) ]# V0 n: A1 l" t7 l
; [5 |, R$ z, d. Sdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
. ]" c% e/ I, d% p( B. q2 Iset @root='f:\usr\' -- Start root7 Q3 q( R( o. l. p% G. [( L Z
set @name='cmd.exe' -- Find file7 }7 j0 Q# C- G) k# i. y
insert into tmp exec master..xp_dirtree @root,0,1--9 j/ A1 @$ L# [5 y4 Z; Y+ [
set @id=(select top 1 id from tmp where isfile=1 and name=@name) ; p$ i+ b5 X6 m X
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)# h* j. s0 T. A0 n) e% O* q
while @depth<>1 7 v! b5 A2 `) o; ^% w$ ^/ I
begin
# L7 J5 o0 f. Y7 @; k& Iset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 7 ?5 Z# x; |/ O# `. v
set @depth=(select depth from tmp where id=@id)
4 H5 Q8 S0 A* m7 y$ A( F# r! `; Yset @name=(select name from tmp where id=@id)+'\'+@name" @8 r U) o! ~7 t0 R/ M8 x( U
end! l! h" ]9 g$ ~* A6 k& ^& n; q
update tmp set name=@root+@name where id=1
2 d _* u: n6 }/ [" [% p4 `! v3 \select name from tmp where id=10 {6 v# G/ r8 O/ A1 y( Z9 x; r$ ]/ z
3 p7 P9 P" X# M S, n0 R4 d查找目录的语句* I$ Y# g3 i; j% K# T, N4 v4 C/ ]; ^
& e4 t$ j: X2 W! u8 u' m* N+ h, |/ B. }
CODE:
/ e m- A% l6 V( Z# [+ }1 w' Y- c
' \% A' Y F/ s( v- y1 j9 {/ i4 s+ e; X$ z' E
drop table tmp;
* b' J( W+ E9 w" w* p7 _create table tmp
3 T S: p2 X0 c" \% y/ d2 `(
/ s) |+ H7 `2 B' l[id] [int] IDENTITY (1,1) NOT NULL,, N. d! `9 {$ a) T
[name] [nvarchar] (300) NOT NULL,% M/ b: E4 R. x3 N; O* ]4 Z
[depth] [int] NOT NULL
: c9 N) X6 @: B( B/ \7 G# K4 e);
/ j0 I; W( \' T: l9 @; C7 t, k2 V* m$ W
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)! |4 } `' l1 ?9 u2 U& A5 S0 Q
set @root='f:\usr\' -- Start root
) o* }- o6 |" r- E: ]set @name='donggeer' -- directory to find
; E8 I9 R( V; h4 F; Pinsert into tmp exec master..xp_dirtree @root,0,00 H7 v6 d7 @7 W S5 c
set @id=(select top 1 id from tmp where name=@name) + J, W" M! H5 b+ Y/ k' q
set @depth=(select top 1 depth from tmp where name=@name) / Z2 f8 n3 G% j7 X% K: E8 K
while @depth<>1 8 [# y0 G, T6 ~% Z8 \
begin ) Y) X/ U% d% k) a( y; u
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc): w2 l* C- _% M
set @depth=(select depth from tmp where id=@id) ! H) ^# r$ q- e. }% w% k, ]/ \
set @name=(select name from tmp where id=@id)+'\'+@name 9 G; p# R* Q( M
end update tmp set name=@root+@name where id=11 |: x5 ^& I+ a# v$ y- Z6 p% c8 g
select name from tmp where id=1 |