找回密码
 立即注册
查看: 2565|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
: F% h; p' f( q' J! S
" a5 h" A4 ]4 o' ^. TCODE:- T" E/ o+ e1 g" X' o
4 z9 {$ m/ }* {( H: ^
drop table tmp;! i! D# c1 j  \" _4 c. I3 n
create table tmp
: l$ ?% L) r- ^7 O% ~(5 e8 S+ C9 K% T/ }& ^5 e# L- t
[id] [int] IDENTITY (1,1) NOT NULL,
' x8 T2 T& H) u1 A[name] [nvarchar] (300) NOT NULL,
' i* N/ f6 e9 B0 k/ U3 \[depth] [int] NOT NULL,5 w8 N% l# H' N3 B7 @0 V
[isfile] [nvarchar] (50) NULL. X8 V' P2 j9 u
);
6 F4 Q% r7 D! @- P# [
6 `' B: {" }2 cdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)$ f) H% |2 }7 Z# A1 K3 x% w
set @root='f:\usr\' -- Start root
/ u( J! P. |8 Y, b3 [( @set @name='cmd.exe'   -- Find file
" n# i  }  K2 _" ?1 _9 q6 V' Zinsert into tmp exec master..xp_dirtree @root,0,1--
- ?8 c( J1 M1 H4 a8 b- Wset @id=(select top 1 id from tmp where isfile=1 and name=@name) 6 G# _' h+ _, q) _
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
: j  M0 D; Y5 w% B5 Z5 Uwhile @depth<>1 : j2 S1 e" k' V  ^
begin 3 r) ^: n6 R+ Y0 d
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
9 H" ^0 f) L5 [6 Z& W- S  {8 M% _set @depth=(select depth from tmp where id=@id)
3 W$ o$ N0 @. {" d: c9 }set @name=(select name from tmp where id=@id)+'\'+@name  N/ k, I8 Z. r2 P7 Z" g+ U6 Y: _
end2 \' M8 Q$ [; i" j; |
update tmp set name=@root+@name where id=1
9 B: x9 Q7 k* d$ f$ qselect name from tmp where id=1: J- \' B# V; m' j8 y* J! e6 G) b

& `# L; ~+ Z8 H0 [$ V" S$ o7 ^查找目录的语句: c- ]/ L8 @2 k
& k9 m9 ^" l4 t) r. Z
+ \' Z% A2 c* H. V# o
CODE:
! `  ~* h9 a0 C1 u3 f
- p- T' E+ g& {" G7 F# k, r9 F9 i, h5 u7 s: Y! B, ?/ j, H# M$ z
drop table tmp;
  C4 j# u7 q% p* J; M6 C) j) Gcreate table tmp% ~+ A$ G. _; m8 M/ |9 ]1 C
(' S7 Y* M" T6 C' u9 |1 ?, H
[id] [int] IDENTITY (1,1) NOT NULL,/ D: u4 z' H( v# W$ F# W& [
[name] [nvarchar] (300) NOT NULL,- k2 q- N8 u7 _3 ^; m6 e
[depth] [int] NOT NULL' V& H, a  r, C9 d  Z, \
);3 P4 Q) Z. y' m. v7 y9 H0 v

5 x; h; [, ^, J& v! ydeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300). v" G" z6 y! N2 B
set @root='f:\usr\' -- Start root* L! b# s( g' O4 m7 f
set @name='donggeer' -- directory to find( c: e! E! `# }0 r- M% c' W
insert into tmp exec master..xp_dirtree @root,0,0
- u3 [2 d, c$ B! Q4 Rset @id=(select top 1 id from tmp where name=@name)
, P6 L5 Y' }7 Z+ A! m5 X3 o/ hset @depth=(select top 1 depth from tmp where name=@name)
) \) H7 j: l3 y# uwhile @depth<>1 % v9 D/ B! L) W/ m: z
begin
0 i' G8 b/ ]; w/ u9 D! i! sset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
* z0 ~- ~" E! M9 x1 v7 E" J: c5 l0 gset @depth=(select depth from tmp where id=@id) 4 p! R$ [. T0 N' A9 i. g! N0 U. z
set @name=(select name from tmp where id=@id)+'\'+@name 4 M% d0 G/ C. h3 k
end update tmp set name=@root+@name where id=1
8 C3 H& P( _7 f/ F& ~select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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