找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2348|回复: 0
打印 上一主题 下一主题

快速度查找文件和目录的SQL语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
: G! x1 h2 |: B: R- P: K. F
' y' u) ]( [; c% ]2 U" bCODE:! q- i0 d' h5 u7 I& {0 N

: A6 m9 q- k& Y' \9 c8 Z1 Y8 [drop table tmp;+ ^1 j7 p3 `) B
create table tmp
: [3 Z6 t/ e5 I* O0 R8 y0 o(
# ?- |! m7 E4 m; x; _[id] [int] IDENTITY (1,1) NOT NULL,
/ a: ]  E  i" p* H5 L& K[name] [nvarchar] (300) NOT NULL,
& p5 c. ~! r6 |8 G% V& i7 y7 m[depth] [int] NOT NULL,1 O* X& C2 d& `! v  b
[isfile] [nvarchar] (50) NULL$ r! K7 c4 H4 i3 y, S
);
  f, k3 t$ R/ {6 n
# C4 J5 e2 B# R" ?: A2 Sdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)! F2 B! }6 n, v$ o( F
set @root='f:\usr\' -- Start root
2 k! \4 \7 ^: k+ z* w, [0 a. ]set @name='cmd.exe'   -- Find file2 c* O! u. j$ P, W, o/ Q/ X
insert into tmp exec master..xp_dirtree @root,0,1--
( Z1 R  @* a( _+ u& w) ]2 Lset @id=(select top 1 id from tmp where isfile=1 and name=@name) # G7 J3 J- a1 |1 G  N
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)0 K2 C# Z: d. V: K& g
while @depth<>1
! z% L2 j: V( y3 R" `begin 3 {8 E8 t8 \. R: a+ X; }* E
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
0 G! ~1 T$ [, ?9 @4 P6 X. Vset @depth=(select depth from tmp where id=@id)
. w5 {5 x: f& L, |! cset @name=(select name from tmp where id=@id)+'\'+@name
2 z" F) `) T+ t: l$ _end
$ f- X! r- D% Rupdate tmp set name=@root+@name where id=1
( b/ D$ h5 Q. N9 ]+ O0 b6 S* uselect name from tmp where id=1! r' Q! ~; b! b& m" I5 s% j3 w  w

, L4 L9 Q# X+ s/ }, U9 @3 a查找目录的语句. R" M0 T, l* X  E+ l% \
% n5 o5 K3 ]9 }! `

# q0 p3 i7 N6 ]4 M# E  K) Z/ F' OCODE:
1 H  m2 e% _1 c& o' V( g8 w
/ r& y1 }; t: F) Y+ l! x' G2 J5 H4 F; r4 Y5 O; T7 Z! A
drop table tmp;9 L2 U4 L6 {+ c0 G& j: i1 |6 A
create table tmp
+ J0 m* S1 b" [( B7 @(
/ D6 ?4 R$ K  G' L5 k/ W' ^: {[id] [int] IDENTITY (1,1) NOT NULL,
* g( q3 \$ n7 v/ |5 a[name] [nvarchar] (300) NOT NULL,
# N) D% t0 N+ z[depth] [int] NOT NULL
  F: T1 L( T: _& T/ U, U);
: Y  ~# c* X0 C  E% w8 Z
2 X) ~! j. X9 i% y' pdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300). Z+ U# |3 U- C- X) n  _4 \
set @root='f:\usr\' -- Start root
- T% k" {% ]% B( tset @name='donggeer' -- directory to find" Z  u/ I9 a$ P7 @. p% }' ~2 K8 q
insert into tmp exec master..xp_dirtree @root,0,0
2 O) t( s. d/ dset @id=(select top 1 id from tmp where name=@name) 8 c' V7 K1 J1 s
set @depth=(select top 1 depth from tmp where name=@name)
7 X  b  ^0 ]6 a: t- F1 S0 ?while @depth<>1 / g% d& S2 i1 ?! V, L! D
begin : J: j# I; \7 U( r2 z* z: B: X
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
8 g1 G5 s+ V4 D" iset @depth=(select depth from tmp where id=@id)
+ r. f- k# f1 c( g! fset @name=(select name from tmp where id=@id)+'\'+@name ' n8 n" K# F1 d; w# j' p# m' @- ?
end update tmp set name=@root+@name where id=1; M4 X! g9 p  m# U
select name from tmp where id=1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表