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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
' g7 w! I! ~1 n) s. h3 d( S* ?0 `# g
CODE:
# t: s/ Q" N. e0 u8 p6 x/ a% ~! q4 E
drop table tmp;
! `6 R! T# `- wcreate table tmp: h8 f1 f3 p! _; d) K1 q  Z' c
(% a1 g- ?+ G0 V6 m! H
[id] [int] IDENTITY (1,1) NOT NULL,7 W7 P; l( S. q7 }+ N
[name] [nvarchar] (300) NOT NULL,7 k" K* ~* B+ a# Q0 v4 b
[depth] [int] NOT NULL,- M1 q. [# |1 W0 Y) F  m- o
[isfile] [nvarchar] (50) NULL0 i5 }: v  Z$ N
);
' G" r; v$ n9 N4 m7 B5 T, `4 L- b7 n! ~2 u! Y3 z$ G+ r- u
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
; W( g  C5 z# Y; o+ Z+ F. _/ zset @root='f:\usr\' -- Start root' l; A7 S8 v- N/ x4 x
set @name='cmd.exe'   -- Find file
7 T# T0 r- e0 q0 i: w3 R0 r# Pinsert into tmp exec master..xp_dirtree @root,0,1--: k! |* G5 c) i7 s/ a# Y7 b
set @id=(select top 1 id from tmp where isfile=1 and name=@name) % K% D: d0 L$ {' N
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)$ y+ z9 y- h$ ?$ U; ?, K: i% b
while @depth<>1 7 j9 d' f6 T' ^/ p7 j9 T
begin   W; k$ k: {; h7 L5 ]( f3 C1 F# g
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) : ~7 G; I3 k+ ^# s9 M
set @depth=(select depth from tmp where id=@id) $ O1 ?: t% ?, O5 N1 R' ?
set @name=(select name from tmp where id=@id)+'\'+@name
  \1 ~& J' }- O, y4 S: zend
' S7 Z; x. }  D) U2 r( Y3 ]6 gupdate tmp set name=@root+@name where id=1- m0 ?6 t: E* {4 M
select name from tmp where id=1
) |/ N& v+ `' ^" N* Y
! V, O4 j! L3 t& W查找目录的语句0 ]' v0 R, h% O$ Y8 j* W( L
8 G) a, M3 K7 Q  u( l* G/ ^4 T5 \

% m" _( V6 p" SCODE:! p: N" B7 I( ?2 `7 H: |' ]/ J' h

+ U$ h  p0 @& w  T& z# j; t7 g4 [# d- v5 Q4 l
drop table tmp;" h5 S; v# j, s9 b: {& r
create table tmp- f% b, P" C7 _4 E# d# e
(" N8 O6 F9 x. {9 A* Y( |8 I7 C# H, q
[id] [int] IDENTITY (1,1) NOT NULL,) s1 h: c' k* O1 x- v" S, }; P
[name] [nvarchar] (300) NOT NULL,+ K3 [; V4 O$ l: z
[depth] [int] NOT NULL2 q. _) k# `# w& u! E, ]
);* O* |) m- Q$ F; }- S
: U6 N& z7 ?0 r5 B9 W
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)& i4 i, L$ p. G$ @, ?% n
set @root='f:\usr\' -- Start root" ^" a7 x8 A8 y
set @name='donggeer' -- directory to find
2 Q. F) Y1 E% s  i" winsert into tmp exec master..xp_dirtree @root,0,0
( s) ?4 U, i) u8 z. U, s6 Xset @id=(select top 1 id from tmp where name=@name) " M3 S  {9 E' {- _0 u2 O
set @depth=(select top 1 depth from tmp where name=@name) 0 o) \4 ]& q0 h3 w6 t
while @depth<>1
# Y5 R* q6 J5 E( @* _1 d" L) M/ h) Wbegin
; u9 e+ o  M; kset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
9 z% c9 q5 ?3 k, Zset @depth=(select depth from tmp where id=@id)
4 r1 S  R% S' B; p+ _8 }set @name=(select name from tmp where id=@id)+'\'+@name + L: T- ^# A. \3 [! X
end update tmp set name=@root+@name where id=1
$ ?( C7 s: l# i/ ~& @' u" _select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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