查找文件的语句3 r* C$ P+ _7 Q7 ^8 A* U
4 z/ F) \ f5 F: ~" d: V8 o
CODE:
7 j: Y, X n2 _; `
1 `, B Z2 U$ S' q' Ndrop table tmp;
. n: `7 S. K; R/ _ Hcreate table tmp/ @- F% r# a* H( w+ S0 h0 S0 b% r
(* F: a7 z F9 i/ {7 C. r- `4 D2 Y6 ~
[id] [int] IDENTITY (1,1) NOT NULL,) i2 L6 R$ B7 c( B, ]0 V' e2 \
[name] [nvarchar] (300) NOT NULL,
, l& x8 V9 F' Z9 {3 K, Y6 I3 [5 T[depth] [int] NOT NULL,: y; M- J2 Q5 @3 V: b
[isfile] [nvarchar] (50) NULL
6 k; Z2 ~" F$ A K" R);! ] N; o: }6 C. a
4 L- A# X0 m- i, n
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
5 l- o0 m( A* T% n/ ?3 oset @root='f:\usr\' -- Start root
8 R4 r. i4 S9 u: P0 m4 Iset @name='cmd.exe' -- Find file9 S1 N' F: m4 j/ w ^+ v+ V
insert into tmp exec master..xp_dirtree @root,0,1--
6 Q' _" q4 `7 s0 ?! C7 Y; S6 v, Vset @id=(select top 1 id from tmp where isfile=1 and name=@name)
4 U+ c: y8 g! b9 m5 w: F3 Jset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)5 v: B8 I5 N2 ^; V& P" i/ L
while @depth<>1
U/ y9 O$ ~! F6 p& wbegin 5 o: g0 I/ e4 M2 i$ G6 S
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ( Q" |7 @! H Z" H) \9 H# }
set @depth=(select depth from tmp where id=@id)
0 j4 o% a% c6 x Eset @name=(select name from tmp where id=@id)+'\'+@name v! G; O, n" n
end
: j" v' V: T: k3 M ]& {8 Wupdate tmp set name=@root+@name where id=1
- ]7 P: n8 D! d) Q& _4 k" O* @* Rselect name from tmp where id=1: e# E, S1 u' u: y2 {, B- u
% b& w. I+ x: ~7 |( Q! P5 x) v3 K查找目录的语句, B4 G% g6 @4 R, \" X/ J) E
& _3 O; o* w( ^. m
( s1 ?* ?# [: A, j' D4 |& Y
CODE:
( D/ q2 f& ^( q: m. @" W( Q" n2 ?2 T1 L0 [# m
$ E4 {# J- q% c# ^
drop table tmp; f! I q- P: ~% J i6 Q
create table tmp: r0 }; @" z0 j2 c5 a
(4 @: O1 R9 F# Q# g' V4 h
[id] [int] IDENTITY (1,1) NOT NULL,
+ M/ G! G4 H, {[name] [nvarchar] (300) NOT NULL,( R R( _& }$ T0 t/ \
[depth] [int] NOT NULL
/ X2 q! H; S3 l! m* r);
* p% X7 h; a* v/ l5 w- t- r( i" I* X, H B2 A8 _
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)$ X$ T+ v9 l+ U" V+ Y& _! {
set @root='f:\usr\' -- Start root$ [6 Y/ t v- @" A x& @" I5 F
set @name='donggeer' -- directory to find4 ]' E/ z4 Q6 e
insert into tmp exec master..xp_dirtree @root,0,0
) q1 a& q7 S- E9 B# Dset @id=(select top 1 id from tmp where name=@name)
% B1 J5 p( m# B/ a3 O- ?0 {set @depth=(select top 1 depth from tmp where name=@name) , N/ q$ d1 l7 E4 H& |
while @depth<>1
* g/ K8 }- B( y8 c2 ?/ J, Ebegin # z3 C; a: G7 ^" _ O
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
$ J! ~' O* Y: \1 m( Cset @depth=(select depth from tmp where id=@id)
6 m: o! R' ]# [4 }, M# Kset @name=(select name from tmp where id=@id)+'\'+@name
' `( t9 S# |) x- ]9 d3 h X& Pend update tmp set name=@root+@name where id=1
! w' d1 q4 e2 ]4 G2 j; b0 Aselect name from tmp where id=1 |