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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
3 ]. P' m4 U3 E$ W" y' o/ n6 L$ z" A3 m- y+ `2 W) u. O, g- z
CODE:5 v1 Q  ]9 I' {* ]$ Y, c3 r: R

- N' J& e" s1 K6 e. @' wdrop table tmp;6 A5 n# f! |/ c) ^" g/ Y: y: t8 s
create table tmp
% L- V4 _% }) o: V2 K(9 e# I# l: z7 V
[id] [int] IDENTITY (1,1) NOT NULL,
6 S8 w, Q3 J& ^& {! r) O0 t- U[name] [nvarchar] (300) NOT NULL,$ w% z) J# ?8 Y( G
[depth] [int] NOT NULL,
/ M5 |1 Q. U2 c[isfile] [nvarchar] (50) NULL  m' y# V8 \, @0 y
);- h/ d; u% E6 M$ A6 N8 `0 F
* b: o& ^0 |% g1 s* W2 {
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
) r6 S/ R: g% R/ w  k+ }2 Dset @root='f:\usr\' -- Start root
) O' x8 \6 ^' Q; hset @name='cmd.exe'   -- Find file
# U% A! Q' P/ c% b8 d* Minsert into tmp exec master..xp_dirtree @root,0,1--
" l+ R- V( }8 q+ D7 I$ P5 ~8 X7 fset @id=(select top 1 id from tmp where isfile=1 and name=@name)
: ^& f# O) z+ ^2 [* Sset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)0 v5 s9 ~4 z- q% ~
while @depth<>1 , P. d. R( {+ a# S( E6 J
begin 5 \# T) Q/ L7 r9 w% z# c
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 6 h. W8 n7 z& @8 q
set @depth=(select depth from tmp where id=@id) 5 `+ O/ s+ A! i2 c$ z7 [; V
set @name=(select name from tmp where id=@id)+'\'+@name
0 S* D8 e) X8 d( h# @: k/ X# r# T* rend# S' v0 Y% g3 V. N& h
update tmp set name=@root+@name where id=1
7 r0 C! H7 H: v6 ~- H5 A, c4 s3 vselect name from tmp where id=1  E- p4 t3 Q! x% f/ D" ^" Q
7 |# M4 r% J& G- ]6 V" y) n& R
查找目录的语句
' U5 S% M( f$ E8 j$ q3 k- ^* I9 [+ b  X4 B. y" ?: w+ T

- f# F8 b, X0 W% x4 Y' O( \CODE:
7 V% D$ m# T3 a3 r' o
; W% j3 A; _( R- S2 l3 L( C# {- a7 @% Z! Z* }
drop table tmp;' W. U4 @3 g1 n# n. [
create table tmp3 Y* m; V0 ^) y, X6 I
(
5 y4 @. f- a0 ], C7 E[id] [int] IDENTITY (1,1) NOT NULL,# u  }: j6 H: q6 _: E
[name] [nvarchar] (300) NOT NULL,
5 I- B# K9 C1 ][depth] [int] NOT NULL6 ^' D4 }4 }* |1 Y
);7 k; m" Z; R! `+ v
, L# g+ s. J4 q' t" \$ E1 s5 L5 t( G
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)2 X1 A  O% ?0 k% ~7 t7 H
set @root='f:\usr\' -- Start root
0 r  L! J# l: P; H9 yset @name='donggeer' -- directory to find
1 p4 b4 a0 A- `2 T. P# ^  M! xinsert into tmp exec master..xp_dirtree @root,0,0
( b7 M9 }% s* t) Z+ ~- P: E$ V( iset @id=(select top 1 id from tmp where name=@name) 9 u! V  k+ y7 Z# z* B9 o
set @depth=(select top 1 depth from tmp where name=@name)
7 _8 i+ T  C7 W, L5 V7 jwhile @depth<>1
* C$ U, r# {" R0 l+ S7 Wbegin
) i  D6 U6 h5 H% i5 z- Lset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
5 ?8 c& ~( O/ y" [; ~set @depth=(select depth from tmp where id=@id) 2 M/ \5 Z2 G; [& s
set @name=(select name from tmp where id=@id)+'\'+@name
0 q. Y! b% [, Q: Eend update tmp set name=@root+@name where id=1  _) M- v. D/ ?* Y1 c
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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