查找文件的语句
, X+ {" A j7 B: w6 \6 E& O$ B6 e0 S/ q4 g
CODE:0 b% R- }8 D8 M; E/ [# L" f
9 F$ l, w+ v; fdrop table tmp;
" f4 Y$ x: R1 @/ I. T; `create table tmp
& V' e7 j4 R8 Y/ q! p1 M4 }. I(
- y' W" Q" u7 h S* u" B[id] [int] IDENTITY (1,1) NOT NULL,5 j' V7 `1 ?/ {: r6 h
[name] [nvarchar] (300) NOT NULL,
. [; Z" p( S) z- F[depth] [int] NOT NULL,8 {3 {, P0 D q) V* P
[isfile] [nvarchar] (50) NULL" R/ i6 B9 C" ~# D# d2 Z
);
' t8 ~: i: f/ \9 B- y2 z2 ^% L9 g7 Q/ R. S
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300): H |5 G g0 {' ]1 T
set @root='f:\usr\' -- Start root
" x) Z6 Z& C+ wset @name='cmd.exe' -- Find file
: R* @8 ~! s) g% U* Tinsert into tmp exec master..xp_dirtree @root,0,1--
6 W$ ?3 `! Q5 O- i* H, ~, v. oset @id=(select top 1 id from tmp where isfile=1 and name=@name) ! |+ |1 U3 R5 Y; S9 d: d# H" m
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)% j2 o- u* g5 D* N4 t& z% `2 d* w1 g
while @depth<>1
* q7 p0 ]+ @* l9 u4 @' \begin
8 ]# @3 ]. W; F( d3 u/ c6 a3 s2 iset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) % T) b% a1 } j
set @depth=(select depth from tmp where id=@id) / L9 ^0 x- x/ i% ]- j7 {
set @name=(select name from tmp where id=@id)+'\'+@name; S/ i9 ]8 [5 y; n0 `& t
end
$ ]& h/ {1 E5 J4 b& M/ Dupdate tmp set name=@root+@name where id=1+ L0 M- h7 l: m" ]0 I
select name from tmp where id=1, E: {4 C* F! S- D
. M5 i2 g/ _6 @; L' u; _. C
查找目录的语句
, ]; a8 Y" a/ L6 c* O, j, [: f! N' m
/ W9 L6 A. z; Q
CODE:
# x3 T+ n# w( u# a* U" a' g. |4 g; E; ^
% j$ G" {: X n# Bdrop table tmp;: \3 E ?/ T: y1 P0 E1 I- R
create table tmp, X" {0 a$ B, J+ f) M
(
/ [& R: ~% g. z b/ V% j[id] [int] IDENTITY (1,1) NOT NULL,
% y+ w- ]( ^& o& c$ g4 Q: R[name] [nvarchar] (300) NOT NULL,
/ `1 R# N' h3 T& K[depth] [int] NOT NULL
& b3 \5 B( a$ J/ W);. l! I. q9 \" G3 q% Y0 l9 g
/ F8 Q5 _5 c; o& S1 g% K, S
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
4 D6 v+ A; N2 }$ r& i* Dset @root='f:\usr\' -- Start root
1 J1 d3 b! \/ ^1 u I B5 G" Dset @name='donggeer' -- directory to find4 v& L, }* k% O9 G3 _* L
insert into tmp exec master..xp_dirtree @root,0,0
/ p9 s4 _& T& f, ^# Vset @id=(select top 1 id from tmp where name=@name) . H" w* o6 \* D" V g$ T4 z7 o
set @depth=(select top 1 depth from tmp where name=@name)
5 _( S, u; N! ]) w( ?while @depth<>1
/ N2 K* p/ N% e. K! lbegin & Y! \. ]4 t* J ^$ x @# b
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
* R' H4 K9 b' q2 ^0 ]9 Jset @depth=(select depth from tmp where id=@id)
% z/ y' [( y' y4 p6 lset @name=(select name from tmp where id=@id)+'\'+@name $ ~- y# }9 _! G- \6 O8 i1 K# ]/ b% o- P
end update tmp set name=@root+@name where id=1$ K) j3 |0 }( v! k
select name from tmp where id=1 |