查找文件的语句
2 e- r& `/ l2 P7 x9 w2 h( s$ B2 s" s7 q( \* M
CODE:
2 `' X3 O) w3 Y( L1 V/ M- A+ ?' s) Q& g( S
drop table tmp;# H0 \) P" Y, |) f* R* ^
create table tmp& F" m: j; ^; m$ @! E) t1 F
(
/ ]6 O" p) ^3 K[id] [int] IDENTITY (1,1) NOT NULL,
, N- ? K' F+ ^' J( v[name] [nvarchar] (300) NOT NULL,& `4 U% d+ s4 G0 {
[depth] [int] NOT NULL,
. s7 \8 G+ {+ o4 S( f2 P[isfile] [nvarchar] (50) NULL* v+ [* [) D" ]2 ^
);5 |" e) J# Q4 s% B) V: f
B- [( e2 K8 x+ ?% k' y6 P% @declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
- q) `; M8 X5 Z( q1 a$ ?$ Eset @root='f:\usr\' -- Start root4 |4 r* l. ~+ D3 [
set @name='cmd.exe' -- Find file( q9 u; A% U/ u
insert into tmp exec master..xp_dirtree @root,0,1--
" E3 s* b6 H, v/ h$ a' s- eset @id=(select top 1 id from tmp where isfile=1 and name=@name) + L' r3 V2 j8 Y( d# y
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
. }" |' g9 N2 N/ E9 wwhile @depth<>1
( Z; X% J1 e6 i4 L5 e! G; H4 h# Wbegin Z/ ], y7 I2 ]: u1 S) X
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) & D3 a+ C) @' y; P0 V5 R: I; `8 _
set @depth=(select depth from tmp where id=@id)
3 ^/ S5 x, B2 N9 ?; Oset @name=(select name from tmp where id=@id)+'\'+@name
6 I/ j5 {; ]! d1 i% p! [ \end
9 u2 ~4 R5 v$ m1 S; g0 \4 fupdate tmp set name=@root+@name where id=1
4 c O4 B% D% E2 ?2 mselect name from tmp where id=11 u: c& g9 I% [8 _
/ S3 E5 U$ d! _5 G6 ?查找目录的语句1 l! z1 V, j% } n7 ]0 }
, U! F4 ^+ x- s: T
* v! H: j9 v2 _( V6 x6 ^CODE:
3 W) s2 w+ j& z/ V7 _4 f
2 E5 v2 t8 i5 ^& ~
2 G3 [; L _* w! d/ n9 Vdrop table tmp;
$ u* T {1 ^9 W+ u) fcreate table tmp! i' f9 X$ d l. z
(
& i6 W: k. R3 m[id] [int] IDENTITY (1,1) NOT NULL,0 }2 o/ ^. a: v: w
[name] [nvarchar] (300) NOT NULL,0 H$ d# v; M, P! k
[depth] [int] NOT NULL0 \' ~( U* {4 k7 b* G
);$ D4 j) ?, `9 H& J% O6 _
( ~) v! l- g% M+ I& h) x, Tdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300): {' e) K6 n# q6 k5 k( V9 T
set @root='f:\usr\' -- Start root
- K* C2 u# e3 x- c) p+ R0 vset @name='donggeer' -- directory to find
/ g( R# p8 l# ?5 F5 F% `insert into tmp exec master..xp_dirtree @root,0,0
4 P: u" I4 I6 h4 j. `7 M) @+ C2 }) Iset @id=(select top 1 id from tmp where name=@name)
3 F$ d- {0 W3 U* }set @depth=(select top 1 depth from tmp where name=@name)
3 U0 B+ M8 q& M: A4 V9 l ?- n' l! hwhile @depth<>1
4 D! ~2 `" a0 ?! H- u" P: ?begin
2 f8 t1 \, Y6 g2 M4 Z. }$ `! ?set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)! p/ g* R$ i$ j# U, p
set @depth=(select depth from tmp where id=@id) " b0 `8 m" {$ z, H, B* o X. l* M8 T
set @name=(select name from tmp where id=@id)+'\'+@name ( ~9 ` @ [! ^) @) N5 b
end update tmp set name=@root+@name where id=1: | Z) B' h7 a5 q
select name from tmp where id=1 |