查找文件的语句( J- q( R" M1 _9 X* u
# f$ a9 S) [6 N( H) p
CODE:7 ^ A' P' h) Q
1 G" t) ^/ S* {- ldrop table tmp;4 p! l0 c( `' c6 k/ N' X; k8 S
create table tmp2 W) v3 V1 k( r/ ^- K# a
(
5 _7 \/ [' @- `# O5 U! d$ j[id] [int] IDENTITY (1,1) NOT NULL,
7 `- }- N2 @, N1 m. Y[name] [nvarchar] (300) NOT NULL,9 X4 ?- X! ?8 l4 r3 y/ M
[depth] [int] NOT NULL,8 v4 [; T |! p7 b! v' l
[isfile] [nvarchar] (50) NULL. _; o2 a* p/ ]% d# J. V( v
);
2 ^, r. \5 j" [. l' f4 F- a7 J' z, x. h3 n1 A3 C1 s
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
, [: x2 U0 |4 J4 i6 h+ R& e `set @root='f:\usr\' -- Start root
' y+ f* P' Z( B; Q3 C% J( c' i5 Tset @name='cmd.exe' -- Find file9 n% E6 S$ X, y( u
insert into tmp exec master..xp_dirtree @root,0,1--
5 p, e, z+ H0 A; z# K4 Gset @id=(select top 1 id from tmp where isfile=1 and name=@name) : V+ r) H( w' B: y4 W: G v6 U4 m
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)4 F3 H3 W1 b5 n5 ^- z
while @depth<>1 ) X$ ^- ]3 c+ t' [
begin # F( p0 ^" ^) }) B4 ^8 Q& B4 a
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ( L' P4 u& `" m; _7 w
set @depth=(select depth from tmp where id=@id)
v! q! b L' H1 D3 i% qset @name=(select name from tmp where id=@id)+'\'+@name$ X6 w; O+ ]3 \7 R& ^
end! U7 X( O3 W) P+ y' Q3 F8 Y
update tmp set name=@root+@name where id=1
: x" e/ A' \4 t) U9 d% Sselect name from tmp where id=1+ W8 ?# }" I( Z$ a
2 b* a) p, E/ N6 q2 z5 b# C查找目录的语句2 y5 y0 {" i9 a* K
1 X/ H, ^" j3 `; y
6 x$ H- S% |: R7 u" HCODE:% l- S+ @3 e9 O- [2 {
; G5 O+ b' r$ c3 |
" x r- q" y+ T- s" ?) g' e8 H
drop table tmp;
/ A1 n- K' v; ~0 k/ ucreate table tmp
( c1 G q% T! W+ W(' p L1 @( C8 s/ M' p3 ^, e4 A( @
[id] [int] IDENTITY (1,1) NOT NULL,
0 H8 U3 z; j6 C' y& u) `[name] [nvarchar] (300) NOT NULL,
7 k. y% a+ H" H% J[depth] [int] NOT NULL! m' I9 P: Z9 k: p
);- Z% b4 y: d) l4 e
" E8 d( Z( D. j+ U3 ~8 m; n6 O+ j
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
* c* Z5 h; O& k7 z6 Oset @root='f:\usr\' -- Start root) ~: |: @$ A* S, c+ i
set @name='donggeer' -- directory to find( u" J' Q6 s( r3 a
insert into tmp exec master..xp_dirtree @root,0,0) u; v3 W6 f* J7 ^
set @id=(select top 1 id from tmp where name=@name) . E. h5 m5 C, _' l
set @depth=(select top 1 depth from tmp where name=@name) 8 ^- A' P1 P; w2 P# A" E7 j5 m
while @depth<>1 # U% b1 \: m' f$ m& J6 Y4 }
begin
& W7 [: L$ B. U3 ^3 g6 Rset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
: M6 E( F, d; h+ p# O' zset @depth=(select depth from tmp where id=@id)
( U1 Z6 G% P" `; o1 C: i, }set @name=(select name from tmp where id=@id)+'\'+@name ' {! g2 n- v1 u+ f4 o/ h/ I) j$ n
end update tmp set name=@root+@name where id=1
" d7 e5 k+ r' n# ?0 yselect name from tmp where id=1 |