查找文件的语句
1 R+ v9 U+ [% z6 s, c) u! T1 e; q3 k. J
CODE:
: |. r5 A: N/ o$ `8 m) X+ }4 v3 K0 ]& a
drop table tmp;1 H$ d7 S7 ?. Z& o, M9 t
create table tmp( Q9 o1 X$ A- }( B& X( p8 v
(
3 E' k& d& S0 K0 T7 g$ E. \[id] [int] IDENTITY (1,1) NOT NULL,
: ?2 z5 {, }- w6 f9 @- E! v6 h[name] [nvarchar] (300) NOT NULL,) N2 [; E# G, P) K: t3 Q
[depth] [int] NOT NULL,
7 n1 u8 J: ^) `/ |[isfile] [nvarchar] (50) NULL
7 P- k8 Q6 `6 o8 N( U; P( z: P y);9 t3 ]. ^5 z# |7 F ]+ ]) a
* R4 M; H! p- n7 {0 |+ t
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)2 j2 A, u4 b! r* c9 C2 l3 Y
set @root='f:\usr\' -- Start root
/ Z* D' J$ K8 t5 H5 Nset @name='cmd.exe' -- Find file4 H3 a; B; f4 x5 k7 V% U) D$ ?
insert into tmp exec master..xp_dirtree @root,0,1--
% `0 ?/ q* ^; x/ d" @3 oset @id=(select top 1 id from tmp where isfile=1 and name=@name)
- k2 A. S! o+ N+ @2 j3 N7 w& f3 Hset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
- N, p0 `. R2 N* B+ Jwhile @depth<>1 ' ~8 ~0 `) x1 v+ G0 D5 q" L
begin
& W9 F# j1 G: o0 J2 |% Aset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
( K, q) i- {( K' `set @depth=(select depth from tmp where id=@id)
3 ~5 Z1 y6 D P1 ?( {set @name=(select name from tmp where id=@id)+'\'+@name! X# Q2 x" I6 l. v$ J1 ^4 N. Z, v' Z
end
" X' ?- Z# i# P) t6 K+ bupdate tmp set name=@root+@name where id=12 Y0 M7 j5 b2 i1 @! G
select name from tmp where id=1
5 t( z. f' {- U' T, M
! U2 l1 {( J3 h r' E; b查找目录的语句1 V2 h$ k6 k2 J' y9 U7 Z
2 C) Y3 M0 J& G, e, q0 v* i$ j
8 H9 A) S& P) E$ \& @6 ?
CODE:
$ e1 }, h# D, Q) _. ]4 ^! M" J( {3 C6 U& q+ @
0 M0 L/ q) E8 @5 k# G2 ?. b
drop table tmp;1 o4 q5 L% _7 _+ Z' r
create table tmp" ^% q6 u# C9 f, E% G
(
" }: U' C: ~; l2 p$ v1 V' S[id] [int] IDENTITY (1,1) NOT NULL,4 ]" |! g- i: G& X( \& P
[name] [nvarchar] (300) NOT NULL,
+ h0 a" ^# U& W, F+ f( l& ? P[depth] [int] NOT NULL
9 s' k6 x- w' o0 ^) s);( Z# p$ M& h. z* Y. |; ?6 r' d
) M# y4 g, q; \$ N+ E/ S' ^0 x! J
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
& e2 e% A( ^. e* s( rset @root='f:\usr\' -- Start root, H( z' ?7 z# ~7 ^" l* ? i
set @name='donggeer' -- directory to find
4 X/ b0 B: d3 |/ r1 Pinsert into tmp exec master..xp_dirtree @root,0,06 j) c& K. m! Q3 \' Q0 \$ \
set @id=(select top 1 id from tmp where name=@name)
) _$ ?, R% P0 ^' ^5 f8 M' a6 ]set @depth=(select top 1 depth from tmp where name=@name)
5 m* u4 b7 M+ \5 e/ zwhile @depth<>1 7 r# Y* r$ S& k
begin % J9 n: `1 U4 w8 @0 Z) u- O
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)! ]6 C0 l) O" i8 V" M
set @depth=(select depth from tmp where id=@id) ! M, v/ R4 C5 y+ g
set @name=(select name from tmp where id=@id)+'\'+@name
! w4 j$ [9 \8 D+ K* }end update tmp set name=@root+@name where id=1# z# I5 L; h5 \5 U
select name from tmp where id=1 |