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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句# `/ g5 [* R3 X4 i+ L
! r+ S9 K+ P/ {( n# V
CODE:
8 t% O! b) x2 v5 ]* L, c7 k
- j+ S( x$ g6 Ndrop table tmp;
  \: }' G$ h0 B6 _# O5 V9 xcreate table tmp+ h1 y* e7 w' R3 N! [: ~6 f1 ~
(1 f4 X& x3 c+ n% s. T( [- Z3 K
[id] [int] IDENTITY (1,1) NOT NULL,
. y) g* a) b8 A( v% v3 N- E+ i6 a[name] [nvarchar] (300) NOT NULL,/ {: R2 y: ?  j9 R/ M. u
[depth] [int] NOT NULL," U7 S9 x2 k4 {6 ]
[isfile] [nvarchar] (50) NULL( [2 e3 V! {2 p
);1 l+ _0 c8 h5 m
: r; a( g8 u4 ]9 e1 D
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
# r7 O/ P" Y: Z' R( N1 ^' Gset @root='f:\usr\' -- Start root
1 Z. Q- t9 s( h, e! Wset @name='cmd.exe'   -- Find file
8 H' ^0 S1 Z) R8 T' f4 dinsert into tmp exec master..xp_dirtree @root,0,1--' i! N2 c  j+ e" u6 D
set @id=(select top 1 id from tmp where isfile=1 and name=@name) ; l1 O5 f& j$ Q8 p
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
& w& z( T! T1 n" w7 Awhile @depth<>1
# ~' @3 ?8 |& C$ d- G6 J9 V& w4 Rbegin 7 y- \9 P% r9 L3 l4 ?
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 1 I+ j. U4 k. t, p8 c. b8 E& G8 y
set @depth=(select depth from tmp where id=@id)
+ u: ?4 }2 s$ v* }$ Zset @name=(select name from tmp where id=@id)+'\'+@name
7 D! @5 ^; j( m  L. \8 E6 z- z, A! K( wend0 ^- b* B) v4 i* @4 C5 w
update tmp set name=@root+@name where id=1
& g  M( a$ L# |! L# a3 T. d  ~select name from tmp where id=1" H( x; ?2 Q+ k/ v+ _

3 x) d8 J, Q! [  u7 d, M4 _, T! ^+ C查找目录的语句
# W6 c' c; V; x" r# W/ @
+ O; l# l2 l. x' q6 h0 C& s, v2 T  U/ c8 W3 F. t! n8 H
CODE:6 \+ h. B+ C7 y! n( O7 J. z3 }

' l  c0 q8 U: |# C1 J7 B3 Q& z% X# S3 ^+ o7 W" \
drop table tmp;
6 u- A/ P2 |4 H; Kcreate table tmp- V; A. f2 u- n1 y, m7 R$ h" _6 h
(3 l$ n' D3 |* m* Q5 ]; ?5 {% @
[id] [int] IDENTITY (1,1) NOT NULL,, V; d: z3 @! C4 W5 h0 h
[name] [nvarchar] (300) NOT NULL,4 k3 T" u, H( `* R8 v/ b
[depth] [int] NOT NULL
- U0 [" M: }, Z7 V);0 s9 ?' B  G" v" k. ]+ {- C

/ f4 e: p9 P+ ~( cdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)3 x5 ?& c1 @1 O3 D( s5 V# v
set @root='f:\usr\' -- Start root. s, S) }8 @7 \3 X
set @name='donggeer' -- directory to find
6 R* \0 x! y# finsert into tmp exec master..xp_dirtree @root,0,0
, k3 L9 D, |6 wset @id=(select top 1 id from tmp where name=@name) * U( }" Y' N& x% g
set @depth=(select top 1 depth from tmp where name=@name)
/ `) U( R" ], u# I+ p" t1 U1 pwhile @depth<>1
5 Y2 k/ v5 I  L+ u/ dbegin
$ L; G( e3 l5 }0 o9 Hset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)$ U$ e# G+ R+ @; {1 e
set @depth=(select depth from tmp where id=@id) 1 w2 v& y  |( {
set @name=(select name from tmp where id=@id)+'\'+@name
+ |+ W% K  \$ L: S! P: H( ~9 bend update tmp set name=@root+@name where id=1
7 f4 g2 p1 o3 y2 Y. g& i/ `select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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