查找文件的语句# T" \; J' C3 p; @9 D0 K
: g9 f" h; H j& K, k7 P
CODE:( e! `$ c' @; N8 m$ b
7 Q* Y) g. n6 c0 e$ F3 W% O6 ~: Udrop table tmp;
! H3 h1 i" W k9 D9 V+ Gcreate table tmp( X* C' u# G. s7 r1 {& S* p" u
(
' d0 V- R9 L* Z: Y$ K[id] [int] IDENTITY (1,1) NOT NULL,
, i- A$ M/ u! C$ y1 Y" b$ m! M[name] [nvarchar] (300) NOT NULL,$ V1 G. ~! V1 r$ t2 S$ i+ Q
[depth] [int] NOT NULL,
' d% D# r+ @/ K$ r" R8 p; [2 N6 h[isfile] [nvarchar] (50) NULL
8 h) l$ ?. [# p k7 D);
" c- d; j9 P4 A1 p) n: V( k; R, E* y+ e6 _4 d
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
B6 Y( G8 b; K) Z7 v; I8 \set @root='f:\usr\' -- Start root+ |4 I+ e6 \4 _: a% F5 i
set @name='cmd.exe' -- Find file
9 t" c( G4 ~9 G8 Jinsert into tmp exec master..xp_dirtree @root,0,1--
2 M7 W, G) A7 x% y! Xset @id=(select top 1 id from tmp where isfile=1 and name=@name)
; K8 k6 o) [; J; I4 Cset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)) P: d7 l1 m/ t8 z8 t3 h9 Y
while @depth<>1 ; c4 S6 \6 E' h( a) n( f
begin
9 ]: P0 B: G9 _* Z- O y" _& uset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 1 c, n6 N6 A9 m# a6 \
set @depth=(select depth from tmp where id=@id) 8 ~6 U4 m2 _. I$ A1 m' Y
set @name=(select name from tmp where id=@id)+'\'+@name
$ f1 b. c: ^$ i1 yend
: B' |/ h1 z7 T0 R: cupdate tmp set name=@root+@name where id=1
1 Q6 r2 a3 W9 S) p' K5 u2 Kselect name from tmp where id=1
$ c% H4 D. [& u' T4 K5 l
( y3 X7 e! \6 U% B查找目录的语句
1 J5 j7 S* w* O- o1 X ?* E# y$ J! N
) K5 d2 Z. p( I- b
! K V: F! }" O: C; z& `CODE:
$ \0 }% L: l5 s4 t4 H
; V7 W- X: s2 L) r/ { L5 Y
Q0 g3 K1 Z% \7 A1 G% V) S, `! Ydrop table tmp;
$ Z. g# U' M0 v7 y3 |4 }! q$ Ycreate table tmp5 U& p/ T, M. ]. A( u6 N2 x6 E
(
& c" B Q( f5 X: }% {[id] [int] IDENTITY (1,1) NOT NULL,; g! K2 {$ T8 x6 w/ j+ }' e
[name] [nvarchar] (300) NOT NULL,8 R. ?6 }6 ~. x
[depth] [int] NOT NULL
' d; i3 {. \. P9 f8 H6 s);% G1 V# l# _8 j
/ E. X6 F: P0 W; Xdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300): y, [; K8 K+ m+ |9 C- R3 V$ Q- v
set @root='f:\usr\' -- Start root
: ?! P: p( o" k, |( cset @name='donggeer' -- directory to find6 @# b6 Q; e: e' W, k2 R
insert into tmp exec master..xp_dirtree @root,0,0
7 o" c) s. o1 `9 Q; @& v$ ^set @id=(select top 1 id from tmp where name=@name)
m' W8 k7 L/ Z& \3 V# @+ W2 ~$ [set @depth=(select top 1 depth from tmp where name=@name) 8 W3 Y2 A$ I# p; t. Z; d) N0 M
while @depth<>1 9 d! K- |3 t2 `. m8 A& e
begin
7 h+ C" Q T. vset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc). w9 g, F$ v/ A1 o( J
set @depth=(select depth from tmp where id=@id) ' j: s, G- g R, m+ J' p
set @name=(select name from tmp where id=@id)+'\'+@name
; x1 o$ K' q- Q8 @0 P- }! `end update tmp set name=@root+@name where id=17 F. G, X4 f. K
select name from tmp where id=1 |