查找文件的语句' Q) w8 O& a: K6 ^' N7 w! [6 Y; s' I/ Y
) E# W0 a* S6 {7 ] ?
CODE:
7 O5 h* `4 _9 e0 j3 m) r2 j5 \0 w. R; y$ t/ v# U
drop table tmp;
: @! t4 q7 Z; H2 I8 }& O# Tcreate table tmp, |5 l% f5 b# t" \2 p1 |( o
(* `" }& b* z/ W3 g' t: Q" { S
[id] [int] IDENTITY (1,1) NOT NULL,
' X+ d* Q7 z N. f' n! K[name] [nvarchar] (300) NOT NULL,
7 C( b. x, Y- o/ ` g0 `[depth] [int] NOT NULL,
( [0 }; \2 s3 d7 Y( Q2 j; j[isfile] [nvarchar] (50) NULL
2 ]/ a+ |/ y* X3 J, S! a);
6 [) J6 [0 P' S6 t" D
4 P6 k' {1 c& R% G1 b+ i+ ^declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
' J% [9 P3 ^; }9 F! @' a' ]; Fset @root='f:\usr\' -- Start root: H7 E' s( R0 E+ a4 E6 ?; h
set @name='cmd.exe' -- Find file* i1 V! F/ h& P$ x9 C
insert into tmp exec master..xp_dirtree @root,0,1--0 v5 G7 |+ ]! v6 n- n
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
& z" l3 P+ J! B0 m( O8 d+ Oset @depth=(select top 1 depth from tmp where isfile=1 and name=@name). r. c, A2 ?" F# d. ]
while @depth<>1 9 R1 c1 @6 t3 s6 k& e1 I
begin g& [2 e5 F6 j
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
* S$ j$ ~+ ?% A+ c9 {set @depth=(select depth from tmp where id=@id)
! j3 j0 q% D2 Rset @name=(select name from tmp where id=@id)+'\'+@name, U' E7 k* i3 Q6 M" k
end
) X7 }* ]0 W7 K6 zupdate tmp set name=@root+@name where id=14 R" V) Q, C- S
select name from tmp where id=1
. f. w( g+ Y; s$ D
7 ^8 m+ _( \ K2 D. K& P查找目录的语句. D( `* w( i7 A1 }- i2 m9 J$ v
! W# V6 A2 m% Q" H7 w
' @1 @$ Y; j1 _& V4 a8 A% dCODE:+ w8 w2 e+ ^' z
8 ?$ ^% b$ S) e" M+ q1 M2 ~5 d* M* A1 K7 o# J7 e* R/ S% j+ J7 t' X
drop table tmp;
9 O. |6 j( M* g. ^2 @+ \3 J$ G6 ]create table tmp9 x0 e1 g4 ]9 p6 d8 K4 X1 l
(% H% a/ J, g! q% P: t* h* t
[id] [int] IDENTITY (1,1) NOT NULL,
, V" \6 T- @' h, }7 a% ?[name] [nvarchar] (300) NOT NULL,
4 r; o& ]- B2 L2 A7 n[depth] [int] NOT NULL! s& s8 o5 `9 n
);
3 i' @/ h3 o- l- S7 `* u
) h7 _% {; G/ i) _7 S5 ]declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
$ z, p7 ?* C5 y2 Bset @root='f:\usr\' -- Start root
( Z8 a& b% b- Iset @name='donggeer' -- directory to find
3 a- `2 C( h# q; w4 a+ p6 b( winsert into tmp exec master..xp_dirtree @root,0,0. R/ n/ p9 T) v% q
set @id=(select top 1 id from tmp where name=@name)
( t4 v' B# B; X2 J a" y/ Q3 ]set @depth=(select top 1 depth from tmp where name=@name) 7 D& t2 b- q, K! t; Q. s0 y( o
while @depth<>1
( ]8 v/ F- {8 J+ w2 Ubegin ; q2 J- ~# R# u' u7 y: p6 l
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc). W+ v3 n: ?9 l n+ b/ I+ k
set @depth=(select depth from tmp where id=@id)
2 X% K3 _/ J. y, jset @name=(select name from tmp where id=@id)+'\'+@name $ ]/ o" ?' r. |
end update tmp set name=@root+@name where id=1# l6 M# H3 ^8 F1 [$ O) {; V
select name from tmp where id=1 |