查找文件的语句, Y8 o7 Q& c- ~( y" ]
4 v3 v' N; _" ?* o! f6 V% K2 L
CODE:. D0 a" v9 s6 i8 m2 ~8 A8 f
) T" w( ~* e0 o. p( ]
drop table tmp;
, z& p/ g4 |+ ~1 D' E- k" r, e1 xcreate table tmp
8 h U# C4 q+ I/ C7 P* E: k($ c! U+ c' c; Z
[id] [int] IDENTITY (1,1) NOT NULL,
- b$ K+ r( K) |3 ?$ J, d[name] [nvarchar] (300) NOT NULL,4 C5 O$ I1 t& r0 T: G; \- ^
[depth] [int] NOT NULL,
% g2 C' A( h7 z8 X4 F- {[isfile] [nvarchar] (50) NULL Z; V& C: x3 T! Q$ q: {
);$ N9 S4 v- _; w% k- t$ D+ _* o, B" y
$ V+ v; s' t- w1 z' `declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
0 @- J$ j% I2 \" o; Dset @root='f:\usr\' -- Start root R5 B9 z+ {7 |8 R
set @name='cmd.exe' -- Find file
3 e1 n! l4 y5 U7 H; Pinsert into tmp exec master..xp_dirtree @root,0,1--0 {! e- r4 z# m# {6 d2 h
set @id=(select top 1 id from tmp where isfile=1 and name=@name) . F' ]0 w+ c( Q) x0 {
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
& p& V# Q+ u3 t5 ?+ @# F, Ywhile @depth<>1
0 k, R" A8 y4 J, R3 Z- H; ~begin # H$ S' `" i i t2 [
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) , J L3 j0 ~' T1 ~$ p
set @depth=(select depth from tmp where id=@id)
5 a8 P( l0 k4 h3 W7 \# R& iset @name=(select name from tmp where id=@id)+'\'+@name( C, C. U1 ^+ c. P, r+ j
end
G" }' l9 B4 ]7 O0 d {: r" _update tmp set name=@root+@name where id=11 z+ n9 L9 S* u9 Y
select name from tmp where id=1
1 V3 N V6 L* ?6 A# o6 V; E3 G8 v$ [* S/ K7 S4 i7 H
查找目录的语句$ L( m! g! H7 J+ c
6 n5 k: ^; k8 ]6 ~# F
* Q: C) R+ u: T$ g0 dCODE:
& d- M1 q5 ?8 Z. R% ~) `% k# K! [ Y, Z4 Q6 v
# q: I1 d# {' f+ P, xdrop table tmp;( o. _; Y. K3 y, P1 ]4 V
create table tmp; l# d- z) p; O3 u, i
(
$ E: x% a2 |3 x. @ I[id] [int] IDENTITY (1,1) NOT NULL,
* l" Z: n4 v+ b[name] [nvarchar] (300) NOT NULL,7 V0 J0 t7 D9 |4 l# @" X* l
[depth] [int] NOT NULL
{6 o: {6 s, C' z);1 M: `- @8 W( \7 i& Y0 V% t( i
7 B" Q0 `( Z: @& d
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)( I3 O2 \& U" s$ `
set @root='f:\usr\' -- Start root
3 N& s* Y# Z2 w3 `- b+ qset @name='donggeer' -- directory to find% ]! J8 o$ i& B
insert into tmp exec master..xp_dirtree @root,0,04 K2 h. d! z6 _1 \5 C4 [
set @id=(select top 1 id from tmp where name=@name)
, X3 {( ]% |$ V& c( O9 h9 Hset @depth=(select top 1 depth from tmp where name=@name)
, M1 x/ p& L. i5 G2 @3 ?* T0 [while @depth<>1 ) H) I2 q d9 Q$ D4 e' l
begin / _( \, f6 w& \- |0 J/ Y
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)3 b( G, Q j# n8 N% \# k
set @depth=(select depth from tmp where id=@id)
# o' M. C) L* m1 rset @name=(select name from tmp where id=@id)+'\'+@name 9 {, T; a4 C3 ]1 r' N+ z, V
end update tmp set name=@root+@name where id=17 n4 I# \+ J9 e0 S) b& H1 y
select name from tmp where id=1 |