查找文件的语句
) n! K, T; T& P0 \* i* _# k4 P; W, G
CODE:
% e% T! l$ D7 ]: r7 R$ U* H% v/ {; j* r l+ E
drop table tmp;0 w7 B8 C: L& U. O0 Y) w3 s
create table tmp! I" V: [; b$ e* g9 T% J
(6 x$ |6 ?0 w3 K5 o7 G, l
[id] [int] IDENTITY (1,1) NOT NULL,
* {7 o8 p L# t# d( d[name] [nvarchar] (300) NOT NULL,8 V/ f/ i$ m7 D8 ^& J& b( ]5 c
[depth] [int] NOT NULL,+ D* P; q" t/ B8 A4 ?) |# L' _2 d
[isfile] [nvarchar] (50) NULL
7 y( U, y# V* V! ?, z);2 l. H0 S. V: }1 |. V
' Z9 {9 V2 e1 `% {5 \9 pdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
! h% @5 B3 V+ x+ d- Wset @root='f:\usr\' -- Start root( M+ |; U2 I, |9 c4 n. W9 F
set @name='cmd.exe' -- Find file Q# S' O9 l' p+ T8 J: J) L
insert into tmp exec master..xp_dirtree @root,0,1--
. m% Z" B5 P# Y5 h; C! u; C( u* Q" ^, e8 gset @id=(select top 1 id from tmp where isfile=1 and name=@name)
* }% A" R! C' i7 ^( T) Q- Y' mset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
8 X/ @/ \5 M- w7 {7 y# uwhile @depth<>1 ! ?; @8 k# j, u9 \6 t+ Y8 y
begin ; [% C; v3 F$ }! e
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
- k% l5 m5 t1 U/ A4 O* B, zset @depth=(select depth from tmp where id=@id)
' c0 ~( `' ]4 `( _: wset @name=(select name from tmp where id=@id)+'\'+@name @2 m5 c, C7 W' e L
end
! g: v# Q' @3 Z$ Q7 y- q4 _update tmp set name=@root+@name where id=1) a% K( j7 o8 a3 o( Z* Q
select name from tmp where id=1
9 `2 d& M! W9 i& |7 k v6 E# B$ m/ U o+ B3 z5 A8 Q
查找目录的语句
2 F) E! h$ e- O5 H6 K7 b* B! B6 Y6 s/ M; r2 q
( c6 n$ f- {7 }- F, L" SCODE:
( S X# t F# `! r4 h- x% X3 `* }# d& g3 T K* F
/ D4 C9 I- \8 n
drop table tmp;$ R- F/ f& {; R; D7 U3 U
create table tmp3 V* f9 s6 I8 ^$ d6 F, ]8 ^
(; U6 R& m. k7 r$ G
[id] [int] IDENTITY (1,1) NOT NULL,* f% I7 @/ Y' A
[name] [nvarchar] (300) NOT NULL, J: l% O! e! Q7 C* C
[depth] [int] NOT NULL* W; Q8 E. ^4 U' }1 p3 _2 L
);( J; s" h, U" E% B; b' h
$ ~& W: j0 T4 U4 ]$ B" F; {declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
1 |+ d" S& k+ @8 P+ v5 yset @root='f:\usr\' -- Start root# o5 P/ p5 A$ o* A, y/ N4 t* H& m
set @name='donggeer' -- directory to find8 r3 ~" q& a6 A# Q
insert into tmp exec master..xp_dirtree @root,0,0; o+ |9 G# ~% s% a
set @id=(select top 1 id from tmp where name=@name) . N) v8 e$ M p. |) z l
set @depth=(select top 1 depth from tmp where name=@name) : _% B5 b8 w1 |' H7 @; K0 Z* f9 D: K6 Z
while @depth<>1
" S3 k4 E& u- O& `/ N5 s1 n7 kbegin
2 R) h5 }) ?; v5 V3 j; [set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)3 e7 `9 L* x; F0 e
set @depth=(select depth from tmp where id=@id)
5 A7 m7 r+ n, @* Rset @name=(select name from tmp where id=@id)+'\'+@name
: ^4 P1 `, Q1 ^3 K4 s7 fend update tmp set name=@root+@name where id=1
+ Y' W" n5 F+ Q: s1 U; j6 Mselect name from tmp where id=1 |