查找文件的语句
) F+ \9 s0 W2 u3 u6 r, Z4 s9 w! E
CODE:3 u# V6 I9 C! @1 T
3 q9 G. O, w* [6 d5 ]1 B( Y0 xdrop table tmp;% c8 Y& X& a3 Z: W9 u o" p& x
create table tmp
# k) y. ~+ z2 F1 t, `, r$ d `& _" ?( L5 V4 }( N% Y! R
[id] [int] IDENTITY (1,1) NOT NULL,. D2 \. I# B" \$ h6 A* x8 S+ K
[name] [nvarchar] (300) NOT NULL,& l5 L3 t7 d( @- H+ x
[depth] [int] NOT NULL,1 z3 F; j4 {/ i S* g L" N% W+ U
[isfile] [nvarchar] (50) NULL. W x* ?- u, C2 J6 p- F
);
( E# l0 g0 @8 V
( r7 b, }; i# S$ t6 c" Qdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)( H0 f0 S! Z8 E4 D% Z
set @root='f:\usr\' -- Start root
) y! {( @( ~; z1 G' ^" ?set @name='cmd.exe' -- Find file
8 `- V3 B: {! y ]insert into tmp exec master..xp_dirtree @root,0,1--) L) H/ h* B! _+ O7 {2 w
set @id=(select top 1 id from tmp where isfile=1 and name=@name) , x L/ U- I9 \3 d: K0 r+ ~) r
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)( m$ y& S* v/ F
while @depth<>1
8 X- j* g' t4 Y5 [' F- t. Y1 }begin - {7 ^" N- m* T4 d7 [
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 3 ?; H% b2 @7 A( }2 \
set @depth=(select depth from tmp where id=@id)
& v. w( t" D# J+ e' r' pset @name=(select name from tmp where id=@id)+'\'+@name
$ F; x5 j( R! x, S/ g6 d9 L5 hend+ E, L G6 h) e* n I
update tmp set name=@root+@name where id=1
$ O" O" \+ X' o4 Tselect name from tmp where id=1 }# o/ L& y, r' ~
# A7 {! j2 B( k
查找目录的语句5 _0 G3 o! E _! L2 r9 F) F& s
* M9 p1 p( X+ j' F3 l
' Q3 C% y" J% U- e8 QCODE:3 k/ _" d$ c' d! Y0 I/ x6 ^6 f
2 R: u/ `, ]4 p3 t2 J/ ]' K; d: C. r; J
drop table tmp;: M/ B" d; [3 i* ]
create table tmp
$ f: q- ]3 i: k) x0 N/ Z(
" x9 T' F0 ]/ I9 L8 D[id] [int] IDENTITY (1,1) NOT NULL,
! r( S* A4 n! o" w4 z* E[name] [nvarchar] (300) NOT NULL,
! @! E# k/ s' }/ N, [, [. C[depth] [int] NOT NULL) G# } W* o0 o3 z: L8 ^
);% x9 `6 n% P: L' X
) \5 a$ r/ `% q% u5 Y
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
2 r2 X; ]% ^- s8 ^set @root='f:\usr\' -- Start root
7 x3 v4 h7 Q5 k2 V' u+ `+ xset @name='donggeer' -- directory to find
+ @6 J, L/ W( b7 k+ ainsert into tmp exec master..xp_dirtree @root,0,0% a W: u4 M, ], _6 B
set @id=(select top 1 id from tmp where name=@name)
v) u: P3 l H4 tset @depth=(select top 1 depth from tmp where name=@name) - s( s2 W8 k2 M7 r
while @depth<>1 # A/ c0 c& J: m; G. v& n
begin " |( P( j# L8 ^ i, g
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
W) Y2 E! c% E6 b1 D! `6 g) vset @depth=(select depth from tmp where id=@id) 1 J# P6 v# O' P1 d; b$ S# G
set @name=(select name from tmp where id=@id)+'\'+@name
4 C, F5 a8 b! W( `( O+ q2 Nend update tmp set name=@root+@name where id=1
; G- _; l6 X4 S0 Vselect name from tmp where id=1 |