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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句; z2 I" S3 d8 z& M/ B4 ^- j2 x

  G" O% y* V2 _: s, u% eCODE:5 a, I5 [6 R1 ]" K
+ A, b+ m- u/ W1 p
drop table tmp;
' a* X0 }4 |+ Vcreate table tmp
3 @% [# e4 L0 U) }5 s1 W( a(
$ F* d5 d: L. z- ^0 N1 N[id] [int] IDENTITY (1,1) NOT NULL,* ]1 T2 v# {* S
[name] [nvarchar] (300) NOT NULL,
2 Z) [! Y6 m$ [! E# K; ~[depth] [int] NOT NULL,2 C( i4 B7 f& R- D0 L  s# h
[isfile] [nvarchar] (50) NULL/ X  v1 R, y# M  Y, Q
);" W( s( _$ y2 }  |
! x1 v' Y: f# v" C+ e- t; z
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
1 N0 R; p/ G, X: V7 z# C1 Gset @root='f:\usr\' -- Start root5 v' n' l% t7 H/ b+ ~6 a' r
set @name='cmd.exe'   -- Find file
2 o' O: o% _2 oinsert into tmp exec master..xp_dirtree @root,0,1--$ P& o- `$ X" I
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
- y. g6 I# i/ N. b/ i  Qset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)- j/ F) D7 r- P) k7 r" h
while @depth<>1
: N9 j0 D2 _; S3 z, S/ }2 J& {begin
. E7 Y8 N+ b1 S2 p: P8 a. Oset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
+ G4 C# \( f6 _1 P" j8 j5 wset @depth=(select depth from tmp where id=@id)
, ^) [1 S& }& [: n) dset @name=(select name from tmp where id=@id)+'\'+@name
: U' A, H) ]* V$ I/ O" t6 z# D. Q7 c  c' Aend; u% K% y9 P8 D: h! y: ~. {
update tmp set name=@root+@name where id=18 T( B" g( K: k! e4 q! ?2 O/ \' r
select name from tmp where id=1) O4 Q9 i' [4 ~1 ~' ^) ?7 L" }" W/ b  Y

/ X! }/ {1 c9 Y查找目录的语句6 F! a+ q6 b( G* {

7 G' E) B; H: T' z  y# W8 C/ `/ \4 c5 B) U9 ^; ?
CODE:
: h# I; W1 k4 a% D7 Z# _( I
( |; z3 E7 p% x9 @2 B
7 z0 ?8 ?! U9 Qdrop table tmp;
6 `0 F; @7 h2 Y. @/ X5 j: u0 Q- W7 [create table tmp
0 j  M' q4 l) _- I5 e2 r* X8 d(9 E$ C; x  }8 O5 U( `, X# z/ n- a* q
[id] [int] IDENTITY (1,1) NOT NULL,, I  g8 h( c, ]- s  U2 [9 w
[name] [nvarchar] (300) NOT NULL,4 P4 e$ I( N* `2 a  g) `' q% L/ B
[depth] [int] NOT NULL
2 d3 D/ T2 Q3 C/ Z' |4 u) c# _/ A$ @);
# l- R  n1 H" ?7 Y$ \( _# }# i. G- M0 D9 n+ t9 i+ J
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)' ]7 \2 q- }% I+ J, Y& O
set @root='f:\usr\' -- Start root# Y" M+ m4 t+ q& p2 G) {& w* t$ u
set @name='donggeer' -- directory to find
) `6 G  G3 X9 u1 D% Ninsert into tmp exec master..xp_dirtree @root,0,02 Y+ Z% K6 R' N* _& p
set @id=(select top 1 id from tmp where name=@name) 9 Q, m( _3 i# d0 ~: T, k4 X
set @depth=(select top 1 depth from tmp where name=@name) 3 K1 P' v! d2 ?) T+ H
while @depth<>1 9 q$ W8 G4 x' l8 P% e
begin . s5 X$ P, R% D' H% l
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)! o7 n- k% i$ Y# K3 N
set @depth=(select depth from tmp where id=@id)
+ {  M* b- {7 v' M) cset @name=(select name from tmp where id=@id)+'\'+@name
- y; m* y: _4 U+ ^# u! |end update tmp set name=@root+@name where id=11 ]& Z) R+ F+ B3 h9 q
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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