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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
查找文件的语句
# x. v7 K. l) V
6 ^7 w) |# H. \  b2 UCODE:
& k7 ^8 f( [8 k0 V
  B3 p) L4 M0 G$ h1 |: Jdrop table tmp;
/ f5 {/ f0 c: ^1 ]6 d( y; }create table tmp
+ C$ ?$ [+ K8 s" t0 c* X(
5 y+ ^) Z  {4 u. J7 g[id] [int] IDENTITY (1,1) NOT NULL,7 ^! |2 `) h2 F1 j1 Z4 \+ ^
[name] [nvarchar] (300) NOT NULL,$ \, [" f5 L* |7 e2 ?
[depth] [int] NOT NULL," d# c' i+ j- l* E. I# \; F1 r/ [' ^. z
[isfile] [nvarchar] (50) NULL
4 ^( v7 I+ `% {  c3 L8 g: O/ E( L);
- S6 ~, T4 ~, S9 f" p7 N( w5 [8 @: P8 L, M9 v) y
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300). u9 o- J, c7 z4 K
set @root='f:\usr\' -- Start root5 g7 Z! O1 n3 Q* p, I$ q" o1 z3 I
set @name='cmd.exe'   -- Find file
. I$ j6 p. ]7 C% M9 L5 z6 C$ K+ zinsert into tmp exec master..xp_dirtree @root,0,1--# w9 k6 g1 t* F' W2 w
set @id=(select top 1 id from tmp where isfile=1 and name=@name)   s5 Q  g0 P7 g& \' h
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
" ?+ h- g& `* f5 _' k" ^while @depth<>1 0 c' S8 l4 T0 y$ a) U
begin 3 A* l) X, H# x# C- ^
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ; E: G% g/ g5 x, K( Q6 ?0 |. m
set @depth=(select depth from tmp where id=@id) 2 l- E1 r. E' Q- m: V* h5 B) R
set @name=(select name from tmp where id=@id)+'\'+@name8 l6 m+ E4 R4 e: T2 K) P1 l
end- r$ i* x  p( l! P$ X. c
update tmp set name=@root+@name where id=1
. W; q; X& e3 m; e% q# r/ xselect name from tmp where id=1  y# [& i% D6 N2 T
& H( n7 l! b3 J( X* ]/ i& l
查找目录的语句
, v5 W5 S/ D  ]9 q* T8 x2 g" V9 ^& \5 ]- }7 \+ B& O: Q0 a. G7 u4 s% N
1 f1 i" s5 w( {8 r: z# N$ |; p$ l
CODE:! b9 T) H! B, u; x

, @2 V' V3 D# O+ y2 c
' l/ p5 p( i+ \7 a: o5 H' e4 gdrop table tmp;" d/ e/ W+ o/ a
create table tmp1 C2 k4 B. F& }& g7 K
(- W# }; @7 Q5 a. x& X
[id] [int] IDENTITY (1,1) NOT NULL,5 Q/ {- Q) h# N, {2 p& [8 T# }' U
[name] [nvarchar] (300) NOT NULL,
3 K( g% ?1 k3 ^9 }8 }6 x[depth] [int] NOT NULL8 p8 G% C3 @9 J6 s7 S& w1 S7 p& e
);
" j7 W0 q: F/ }& {* V9 ]6 U7 I" r0 b. ]
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
# e5 L' B. n! r' ~, C+ |$ Q  p# Jset @root='f:\usr\' -- Start root" i& }/ F5 E- F; V- c9 @  I8 t- i
set @name='donggeer' -- directory to find
, n  N1 b8 P! M& m% G1 P( j9 D  uinsert into tmp exec master..xp_dirtree @root,0,00 r7 x- g5 ^+ R# l* L  `& I) y
set @id=(select top 1 id from tmp where name=@name)
& C1 ?/ F: z+ U$ S$ g6 z- }set @depth=(select top 1 depth from tmp where name=@name)
0 K/ a8 ]  W1 |. ]' n# g9 b( A5 wwhile @depth<>1
( g8 G8 |, h& [$ c4 Q. j# r/ O. gbegin
" g9 U* V3 S" P0 I1 Eset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
. H0 n4 }( X4 fset @depth=(select depth from tmp where id=@id)   O$ N7 J* R' h5 b; l+ V# ^/ s
set @name=(select name from tmp where id=@id)+'\'+@name
1 _& b) J* B9 h( i' \: R6 `end update tmp set name=@root+@name where id=1
3 T" a" X: _4 w6 g8 d/ _" B2 Yselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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