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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句. B, J: ^* T. I6 E

# p2 z- o( U; x! Y; |/ S3 h6 ?CODE:8 p4 i9 b* J- F
3 b/ o; t7 F8 M
drop table tmp;
% y) M2 r0 k8 Q" \' N2 g8 Gcreate table tmp
: r$ [( v  n7 G(
/ {7 v' Q) |; n. n6 k1 F0 e[id] [int] IDENTITY (1,1) NOT NULL,/ R" Q, P! m) L, C, ?
[name] [nvarchar] (300) NOT NULL,  u1 A  p) @4 b  G' ?) F! O
[depth] [int] NOT NULL,& H' ~: u1 `. q& [
[isfile] [nvarchar] (50) NULL
" \0 A" A! r1 ^1 f);% f: I% d& ]' c# u" F$ x
) h% g% y5 p$ J/ I
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)+ o; B3 v1 L# c8 s
set @root='f:\usr\' -- Start root* C) Q) P5 O# L. n. r' r9 D, _
set @name='cmd.exe'   -- Find file
' R" M* u+ G' J4 h1 E/ S7 Tinsert into tmp exec master..xp_dirtree @root,0,1--
/ K. ^3 P; ~% I/ S+ S4 \set @id=(select top 1 id from tmp where isfile=1 and name=@name) 4 T; R1 U6 I4 W: b* f5 L
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
8 k. n8 a4 a, h. q1 a9 awhile @depth<>1
8 h5 D7 i3 |6 H- s9 i& n: h+ U& bbegin
0 p! x) ]. N2 J& sset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
% ?6 u* W' h3 |& h1 K2 R" J% Mset @depth=(select depth from tmp where id=@id)
( D: U- L/ f& ^3 }4 Q; e& w" wset @name=(select name from tmp where id=@id)+'\'+@name
1 Y0 S2 J+ N# @2 H# W4 mend# a/ F* l& V! }* Q
update tmp set name=@root+@name where id=1
4 P" l1 o- ?: R. o5 D& _+ h2 y! ^select name from tmp where id=1
9 O. I) m' m6 G$ X1 {, n+ D
. x) D1 `6 u: Z2 T8 @查找目录的语句
, x/ c6 Q  `  t* g: e: H! X+ O  }) _3 M$ l7 a2 M: f

/ Y4 d8 K( i0 a7 l* ACODE:
0 P  a, z7 }- z8 S- M& P/ n& h% C! j

% U# Q. O/ y: h  S: Pdrop table tmp;
6 |0 w0 U0 w9 k" i& W2 r) e9 ocreate table tmp3 F9 d! h8 ]" N" T
(% x2 o0 f! e+ R
[id] [int] IDENTITY (1,1) NOT NULL,3 @  |" [3 o3 l! D8 @7 C
[name] [nvarchar] (300) NOT NULL,6 ^7 N% f- W& C5 N# [
[depth] [int] NOT NULL
# n4 f1 o6 E( c- S1 D6 y4 t);4 f6 v! s$ }$ U& o  G% x+ y

; Q  n/ i$ N( G" Ddeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)/ W9 H6 Y3 x6 X# V* v! i
set @root='f:\usr\' -- Start root" @3 O. l: p& p
set @name='donggeer' -- directory to find  v2 r) t0 D. c4 d8 a4 m
insert into tmp exec master..xp_dirtree @root,0,0
1 }0 C, q6 g! |set @id=(select top 1 id from tmp where name=@name) 1 n8 y* R3 G7 C* X' [
set @depth=(select top 1 depth from tmp where name=@name)
  V. y) s; r, G2 p" b/ T" ^while @depth<>1 : a, w5 x; m, d) S! z. ]
begin " G  a) U! h  L8 N) k; @1 x
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
" V8 s- f+ O) d7 L) h0 N/ Mset @depth=(select depth from tmp where id=@id)
% w3 X$ h. j0 f1 l  _& |- u0 k1 Kset @name=(select name from tmp where id=@id)+'\'+@name
' y5 S6 V9 \% j- t* V( f7 send update tmp set name=@root+@name where id=1" i1 E/ p5 g3 O
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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