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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
- A4 c: P6 b9 v1 I
2 Y  ?4 H. w  M/ c  LCODE:8 d0 \3 w6 P! b3 S
7 a7 A% ~( k* V$ a9 o( G6 O+ t
drop table tmp;
: N+ w. R& N+ ^* W! r! H& L; ], vcreate table tmp
( V  U& e( m4 I- [+ w: b(0 P  Q$ \/ y( J0 J
[id] [int] IDENTITY (1,1) NOT NULL,
9 v  M! k- {7 I1 k* G[name] [nvarchar] (300) NOT NULL,
. Y2 T' L# W9 F& H- [[depth] [int] NOT NULL,
$ u" c0 t* {# K# Z. h[isfile] [nvarchar] (50) NULL. f, e; J! s# S3 P: s( p8 T2 D
);: V3 L  ?* z' N0 z, C. Q5 l
  a( q0 Z1 \1 g! x
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)' {  Y$ f/ @9 z% r' i; Q7 }
set @root='f:\usr\' -- Start root
5 ?  Q7 p: j5 {( H' X  n# A+ Rset @name='cmd.exe'   -- Find file2 e7 F/ d( ~& L. y. L; L
insert into tmp exec master..xp_dirtree @root,0,1--
, K: p" q2 \+ Z3 l6 u  N2 Y" nset @id=(select top 1 id from tmp where isfile=1 and name=@name)
8 G6 z: ^+ r* n! ]set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
- ~$ y( P1 a. @$ y' t" Z6 S  W# @while @depth<>1
& X0 b. z, C1 |* W3 J0 e/ abegin 0 T; A% i3 H+ C
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) # \& [) R( C: W2 I) V' @
set @depth=(select depth from tmp where id=@id) " i9 z- _% M% J) g+ u
set @name=(select name from tmp where id=@id)+'\'+@name
' d  V* Y% h6 @, Uend7 a; B) @: {! w$ Y) n" Q
update tmp set name=@root+@name where id=19 o8 V  B/ j5 c7 f9 i7 B& A5 S  S
select name from tmp where id=1( t0 K' t' U9 t3 _5 V3 N
) ^2 R5 z" I$ q
查找目录的语句
, s( [) y  Y) Z2 @6 Y
( B! i6 V' |" R; Y" b
- ^3 }- u4 S# dCODE:: ^+ Z2 ^; X0 b7 Z* @* z$ b

' _( s" o. M& J3 Z2 s' b0 \. j6 x* p9 Q5 [; u( D  b! \. {
drop table tmp;2 @+ L0 ?; A5 ~! A. u; r
create table tmp
" V; S- K3 N) d: h/ X0 z0 Y8 h(* m$ ^% i5 P1 m% D. P
[id] [int] IDENTITY (1,1) NOT NULL,. {% _: {& z5 x5 H# B1 C  }
[name] [nvarchar] (300) NOT NULL,
/ [% }3 ~& u% v. w9 b- f& \[depth] [int] NOT NULL
+ N; L' I5 _6 i0 ?( J- f);. O4 {. l9 X: m4 T$ t1 _

7 ?/ c8 W+ f# s( kdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
/ s; M. I0 J2 L! j# Rset @root='f:\usr\' -- Start root1 v) w# ]# [, P' ~: ~
set @name='donggeer' -- directory to find, B3 Q6 g  Z2 J8 i# E
insert into tmp exec master..xp_dirtree @root,0,0  C8 [3 W' S$ H  a
set @id=(select top 1 id from tmp where name=@name) - ^% o% `0 }! ^( \8 A
set @depth=(select top 1 depth from tmp where name=@name)
! H3 `% u0 s1 n4 zwhile @depth<>1
, g1 w1 q9 N! }begin
9 o4 n7 q# Y) Bset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
4 R8 M  e% q8 @4 pset @depth=(select depth from tmp where id=@id) 8 g0 n1 v1 P1 A& t: |: ?
set @name=(select name from tmp where id=@id)+'\'+@name 9 v' j4 v( {6 k! H% ^
end update tmp set name=@root+@name where id=1( C3 X; C' O$ H1 D* G
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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