查找文件的语句3 I; y; V4 s6 I( L7 O
2 [1 b' S0 M! ?5 Q+ D+ r+ k% t
CODE:) ^: g& v7 G# p, y* g
: U ]; i! V% X0 cdrop table tmp;
8 a- d- |/ B9 T, s/ k q( Jcreate table tmp
N) q( C1 @( x3 Y" P- {1 }(- `5 x; D8 |. f2 g$ t" L
[id] [int] IDENTITY (1,1) NOT NULL,$ F7 x% N; {/ L4 z1 t" r/ p3 a0 [
[name] [nvarchar] (300) NOT NULL,+ W- h t7 y$ j d3 T N' c
[depth] [int] NOT NULL,) \3 k. `3 q, X. |- F3 }
[isfile] [nvarchar] (50) NULL
7 J2 P# x% E4 e* f" B; H);
( d8 g1 z! P. D: ?( m* g
: E- Y0 K; w4 b* p0 X6 s% Tdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
0 D! m( X! a# [* fset @root='f:\usr\' -- Start root
4 e4 Q) T7 F$ w# Xset @name='cmd.exe' -- Find file
/ G: x. Q* a& b4 P0 f- r" j0 Rinsert into tmp exec master..xp_dirtree @root,0,1--
+ E' |) D7 C1 r0 x/ Iset @id=(select top 1 id from tmp where isfile=1 and name=@name) . _2 c1 d$ A, a( D& Q9 M
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
, {& B: ]( q% Gwhile @depth<>1
- C+ M( Z! f% I9 rbegin % ^3 F; v3 E2 P& g; l1 ~) T- n) Q7 W
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) " `5 I( V, {$ Z! E7 m
set @depth=(select depth from tmp where id=@id)
, U* z& D# W2 t3 Pset @name=(select name from tmp where id=@id)+'\'+@name
& k" Q) }4 m: t3 d y! \0 Lend
: z k% N; s$ ~. `: t6 Wupdate tmp set name=@root+@name where id=1
: @/ k! c( t/ m5 ^2 u" b1 hselect name from tmp where id=11 W2 H. a( M* r7 c, o+ N/ V
8 w( I n, V. ^; f& D8 J查找目录的语句
8 Q/ N1 d' p. s8 i- ?
+ T Y9 F; ]& @/ u/ u. C% T( n4 P( b' ]6 i, q8 S! G6 u5 E4 J5 s2 B
CODE:# _7 I# p5 t& y* `1 _4 e! t, b+ ?
+ y2 K" r9 s. I& Q5 S2 g# W: k
1 V' a8 s8 q: K# Z6 s$ mdrop table tmp;) A. x1 ]+ C7 W2 W" m3 v% J. p$ ^" n
create table tmp8 q+ [$ f: H9 F# f3 m) ]0 q" s# ]
(
& ]# u$ K5 T5 M0 }9 j[id] [int] IDENTITY (1,1) NOT NULL,2 [0 m, Y( A/ U7 R9 e X# S& y
[name] [nvarchar] (300) NOT NULL,* d9 J! G) Q& n8 D0 R+ S# V! \
[depth] [int] NOT NULL
' ]7 k6 O" ~5 v);
! ~/ J9 L& k' [' b2 o0 a$ i F
0 v& m- @& _* E: c/ C$ c" [declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
# o( y* w7 O# nset @root='f:\usr\' -- Start root
0 k" F" j0 o. u$ f: kset @name='donggeer' -- directory to find
& N1 d& U @! z* {insert into tmp exec master..xp_dirtree @root,0,0
( n l! M& { C* G7 b0 n) {set @id=(select top 1 id from tmp where name=@name) + }$ N/ R7 V( d0 M! r
set @depth=(select top 1 depth from tmp where name=@name)
8 ?+ W; }" J) p( N1 Swhile @depth<>1 2 V, u4 n( r4 [' I k
begin ' x; r5 k) ?8 C! s& G, I
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)" x$ Z; b! J' ^- [: z; B
set @depth=(select depth from tmp where id=@id) + ?' u7 ?+ X8 z3 j! Z {+ ^
set @name=(select name from tmp where id=@id)+'\'+@name
* S+ B* m( W) C$ G0 h7 O# t4 jend update tmp set name=@root+@name where id=10 W& B' Q4 t8 {1 ?: t& `
select name from tmp where id=1 |