查找文件的语句
/ a+ D3 i- s A3 D% B7 Q1 H/ b( V
1 c r+ r! J$ i3 M+ x+ pCODE:
! d) p: G6 B( G2 G& l
# T1 V i# l: ?% y' N; ddrop table tmp;7 Z1 a) L, O* h7 [
create table tmp
8 v; F! e7 G+ q# _% h5 _% {% H(/ x3 |+ H! y% G% N8 Q- T# C2 s
[id] [int] IDENTITY (1,1) NOT NULL,8 `( f) _- _% t" x% k! X2 D5 k7 B
[name] [nvarchar] (300) NOT NULL,5 b# F; y: r7 I- _
[depth] [int] NOT NULL,; X9 f. A5 C2 H, c9 ]
[isfile] [nvarchar] (50) NULL
" B( l. y% i$ d);$ g+ I8 E/ O5 V
- C _9 d: R% l: m8 d- T
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
8 z: K7 F: B9 o! a# l) Uset @root='f:\usr\' -- Start root4 U. u3 ]# k/ e0 S
set @name='cmd.exe' -- Find file; e" P5 Z- {. \ D
insert into tmp exec master..xp_dirtree @root,0,1--6 o: Q3 J* d( W2 j: m9 @1 v- p. M Y
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
1 S+ b1 j$ T/ N6 K7 r) @" Xset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)$ k! k' G+ {1 E3 y# }5 H
while @depth<>1 % v$ O. F) K5 t+ n- S6 T& p
begin 2 R9 O$ k+ k+ b z, q v
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
I8 O) [: v0 v5 p0 Tset @depth=(select depth from tmp where id=@id)
* s! S: R& ]* r1 l6 L, @set @name=(select name from tmp where id=@id)+'\'+@name
) \3 ^1 Q; g: x j8 iend. Q1 m* r1 x$ ~+ x$ t: ^ x
update tmp set name=@root+@name where id=12 X# B4 ?: P H5 h. {8 E* P, e
select name from tmp where id=16 j5 L7 ?& \ y1 N% J2 J! _
; {4 `' i9 @" x: C' e
查找目录的语句
0 a' d- G9 j2 {- {6 u2 G4 t
' p+ Z4 D) {1 s8 B, o: [) m6 I1 I. R' w) K- }& P
CODE:% q, l% e4 I- J4 W8 N9 n
" W! W% z, `. I% r9 m
1 Y3 L9 v, i# p) a! l8 W( f2 ?
drop table tmp;0 g; Z, H4 S" W, a- y
create table tmp
2 K) J$ E! q+ [; T3 y(
1 A! E4 y$ z5 Z$ L9 F T[id] [int] IDENTITY (1,1) NOT NULL,
- M- v$ w, Q& Y: d0 ~7 L[name] [nvarchar] (300) NOT NULL,
1 M \' b1 A% s4 G& w[depth] [int] NOT NULL7 T! P: C! |" c/ \4 S/ q, j5 t
);
+ g- B5 E" ~ }2 _
3 s4 W; Y$ v' V; E* T; j0 Ndeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
5 J! V$ e6 F) j6 C' y8 Wset @root='f:\usr\' -- Start root N/ u* ]: u9 ^+ S4 w7 v/ N! g
set @name='donggeer' -- directory to find" D7 ]6 f8 R' V8 S1 }2 B
insert into tmp exec master..xp_dirtree @root,0,0: }2 ?: ?$ o! w% q" ]2 o
set @id=(select top 1 id from tmp where name=@name)
' `- U& [6 U) t3 H) ^set @depth=(select top 1 depth from tmp where name=@name) $ q8 O% |& j V& ]( W
while @depth<>1 0 C4 O7 x. Y7 u' F( f
begin ' E3 ]9 R6 g: m! [. P9 c6 ~
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
l; ~: a* t' J: q0 s6 Yset @depth=(select depth from tmp where id=@id)
/ R. S+ G9 h1 T l) c; ~# Tset @name=(select name from tmp where id=@id)+'\'+@name
2 B* M* z3 G: v2 M$ A) @5 w* {end update tmp set name=@root+@name where id=1
3 k I+ h* X$ I. l. Eselect name from tmp where id=1 |