查找文件的语句# S) y4 ?2 u/ g A7 e9 j
3 `, A m- m( Y! ]
CODE:% ~- i2 @3 m1 [1 e
1 E. `' Z4 z0 x
drop table tmp;& Z/ o, l' {! k# u
create table tmp
+ B p: \ @5 k, B, l4 U(0 V- n- X8 U( I- k# g; q
[id] [int] IDENTITY (1,1) NOT NULL,7 y9 I' v" H- E6 _, K0 r4 q2 K
[name] [nvarchar] (300) NOT NULL,& g; m' T" G) R5 A9 i% N
[depth] [int] NOT NULL,
; E4 i0 r! z' w- O[isfile] [nvarchar] (50) NULL) Y; n% h' c7 x$ h# m7 o
);
( P% b$ r/ n4 m. [! q. p
* q0 X6 D3 \, l/ N/ W2 e4 t$ qdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
5 Y! r1 m" Z* n! P' k. p9 s2 yset @root='f:\usr\' -- Start root7 ?) c, L& u" r" j% N# z
set @name='cmd.exe' -- Find file+ o r! [! n3 _: h2 A
insert into tmp exec master..xp_dirtree @root,0,1--: L# x4 H( F2 L0 L% r. I$ Z4 P( o9 O* @1 a
set @id=(select top 1 id from tmp where isfile=1 and name=@name) 0 `4 B: B* `: R0 [8 C5 O
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)% W" \, f6 c( S- u6 B; X- m4 }5 _
while @depth<>1
2 E z! q, b+ C: `+ D3 c) Jbegin
2 w2 y a, i- H5 K; C3 Xset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
; A8 z: C+ n* c) Kset @depth=(select depth from tmp where id=@id) 9 L% [6 A4 h0 A- `1 d- F) I
set @name=(select name from tmp where id=@id)+'\'+@name5 B/ ]; ]* `! m5 ^, M
end n! u& l" V) \; V7 H9 d/ t9 U
update tmp set name=@root+@name where id=1
$ \0 {2 C [ ~0 r3 u& g9 Nselect name from tmp where id=1# T$ {" Z7 q/ u$ Y
- j4 H: ?: |3 P- ~0 O1 @
查找目录的语句# K. ?/ T0 A7 G: h
* J/ V* x. P$ r( Q$ |% I, C7 U4 t. m3 J6 y: B- O
CODE:
$ a$ p' J: o% ~$ T7 f& f
& f8 f' Y4 ]/ K8 T5 [
' ^4 b! V5 u# t0 X: J5 V' k5 pdrop table tmp;
5 h( U+ {' y$ mcreate table tmp( C' H- g* ?) T
(
4 e, h9 S# |! E6 D& A* l/ |[id] [int] IDENTITY (1,1) NOT NULL,+ r- D, t5 Q4 ^) c4 l7 D3 S
[name] [nvarchar] (300) NOT NULL,# I: Y1 H- P. D1 e
[depth] [int] NOT NULL8 q; N( M7 O+ N! H
);2 c6 c: V" E# t4 X% {1 q
' F& d! \9 q+ B! Rdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
; Y( I1 A9 p0 ~; Dset @root='f:\usr\' -- Start root6 Q) t0 Z' B# x- z3 `
set @name='donggeer' -- directory to find
5 } F! ]4 M# d3 Uinsert into tmp exec master..xp_dirtree @root,0,0( g, T, Z9 |) e" W2 s8 j
set @id=(select top 1 id from tmp where name=@name)
5 s! E. K; H/ u; t. ]& `set @depth=(select top 1 depth from tmp where name=@name)
; \; i3 r% K" G+ H, X8 `3 g" pwhile @depth<>1
6 `2 m2 V( i' L; I) Cbegin 8 _. C. Y0 s& u5 j0 C- ]
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)8 h0 y! a8 |5 c9 C* p8 M/ ?
set @depth=(select depth from tmp where id=@id)
g4 ?1 z* A. p5 u/ {. b+ L: Cset @name=(select name from tmp where id=@id)+'\'+@name
) x! w7 e: y1 ~4 o, W/ J' hend update tmp set name=@root+@name where id=1, b) [& U; E$ \8 a' g9 ?' W) J% x
select name from tmp where id=1 |