查找文件的语句
, Y$ g+ v8 U$ A1 L c1 D- N$ K" S' t I g( ]/ n5 D
CODE:" m4 n& S0 b4 F+ K: w% p, M' `3 F/ q
! b4 ]0 ^$ c1 t4 l( xdrop table tmp;
& g. _; {8 W& B& G ocreate table tmp8 n8 L: n7 D) i& H1 @( d
(
, q$ E" b: u! `% e+ o( x[id] [int] IDENTITY (1,1) NOT NULL,
) K: t, j6 T2 H0 z1 {1 S/ x! b+ d[name] [nvarchar] (300) NOT NULL,* M+ n8 u0 k5 [
[depth] [int] NOT NULL,
( }9 o) U: }" U- t$ ?) y5 | A[isfile] [nvarchar] (50) NULL
! n$ j' n$ f0 k- d, O+ ?" I);
% _7 t- p7 v( _2 H- E* F/ x+ J: D) i( K3 X
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)+ j( V* t8 `; r4 f7 J
set @root='f:\usr\' -- Start root6 @# Y' z" j8 K' o9 J! M2 J3 a
set @name='cmd.exe' -- Find file
1 }, k" {# u; y1 N% v7 @insert into tmp exec master..xp_dirtree @root,0,1--( F% G$ W- Y' i. g( p
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
, i$ u1 k7 `4 ?7 eset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)1 h3 A/ W' v Y" L
while @depth<>1
, u! }3 b! z# }7 L# p5 n& Jbegin
8 ]( L% b1 W" |7 G' aset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
$ U# o1 N2 V9 F( Sset @depth=(select depth from tmp where id=@id)
5 S+ _0 y* }0 B8 s# I) qset @name=(select name from tmp where id=@id)+'\'+@name. P& F5 g1 Y$ M
end
$ N ~0 E% L1 O6 o' H6 _update tmp set name=@root+@name where id=1) V" [- W% M9 L( p3 a
select name from tmp where id=1
7 l8 ~$ _. W- J1 m9 q6 P3 y/ `" O2 f
0 b0 r$ C2 _. j& q查找目录的语句
9 D! a5 E6 c3 l8 }1 Y& x( o7 O$ c' R* d
+ A4 P% C/ F: S/ Y x
CODE:
+ o& Q! t8 ?& I2 o8 H. t. ~& E
9 L+ K* R& q* [8 |2 L- D2 j) o( T, D8 N) b
drop table tmp;
9 J- Q8 D: U" Z3 r4 W' Vcreate table tmp1 { R! Q/ R- ^0 L! P. n
(9 H: S3 V) W5 x* {
[id] [int] IDENTITY (1,1) NOT NULL,9 C2 i- J2 r3 G7 N! ?# `7 L" i" X" ]
[name] [nvarchar] (300) NOT NULL,% z7 Y6 a4 R3 U" i4 Y
[depth] [int] NOT NULL
$ O$ x) V/ }4 |' U);& g6 t. D$ I3 b+ \; g" x
4 V k2 h5 I( E
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)* _+ i: j7 V& F
set @root='f:\usr\' -- Start root
0 P6 k+ M/ }- `: [- rset @name='donggeer' -- directory to find
e7 J: V- v3 I1 j6 S, {. Xinsert into tmp exec master..xp_dirtree @root,0,07 c5 g' D2 \1 Z5 R0 K
set @id=(select top 1 id from tmp where name=@name) 1 O6 z- I9 |* d _; G, k ^
set @depth=(select top 1 depth from tmp where name=@name)
3 ?: f# p1 @2 D' o X8 z4 iwhile @depth<>1 # @; ^4 M. r5 b
begin
. G, E; p) x# l% j. s+ _. r( Qset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
4 @& ]( x' p: D! T/ C: g8 Mset @depth=(select depth from tmp where id=@id)
4 t2 \) {) | `% M- Eset @name=(select name from tmp where id=@id)+'\'+@name
8 S% l3 `' ~3 c% g2 F: m' bend update tmp set name=@root+@name where id=1
. a5 e6 e0 }4 F3 v4 r+ }' }7 Oselect name from tmp where id=1 |