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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
0 L4 B5 z# ^9 `2 T- v* N! b; J! J+ e' n5 u4 \( D& }
CODE:) e- p* _" F9 K
; m8 P) O" u- M9 W3 B. F
drop table tmp;
* |  p. `2 ~. o! Wcreate table tmp! n" B. N0 X" l  k. ?
(5 {# ~+ d$ c2 o+ h, N
[id] [int] IDENTITY (1,1) NOT NULL,
- |6 c0 U5 j; u[name] [nvarchar] (300) NOT NULL,( s! m& L7 \( x3 M- H1 `
[depth] [int] NOT NULL,2 _* a8 ]* T3 ]0 L7 o+ P
[isfile] [nvarchar] (50) NULL4 o$ I& F  i# x9 s) }% z
);
4 Y+ N) g! r6 ?# X% P  z: w+ T3 F# x
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
" s# i: p5 y5 _1 j3 D& U% pset @root='f:\usr\' -- Start root" w: C! r+ L$ |/ ~5 ]% W  L
set @name='cmd.exe'   -- Find file
6 \. q" ~2 X/ J2 zinsert into tmp exec master..xp_dirtree @root,0,1--  Q# i9 _1 o4 r! ~( K5 z. A
set @id=(select top 1 id from tmp where isfile=1 and name=@name) ; T" i& q! c, O
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
+ l( [# ~# A, ~9 B1 L# dwhile @depth<>1
! X5 R  r$ n" b# b6 f  Wbegin
4 Z- \7 q! B' h" p+ Jset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
1 q. r' A+ c& _. r# u- g+ \; m. F( Sset @depth=(select depth from tmp where id=@id) * C- Z0 @/ {. p0 @2 u  K8 p
set @name=(select name from tmp where id=@id)+'\'+@name
$ T& h1 l5 j0 ?; O+ K2 Dend
8 h0 U+ M# {% M/ h+ |update tmp set name=@root+@name where id=1# i, e. S9 p4 \+ x" W/ L; J! \/ }
select name from tmp where id=1
' [, d. Y/ `: e2 w( I, s- U' F- u) Z. O( c1 X# G; `. c
查找目录的语句) Q% `$ t8 K( p

/ C% n# V/ w8 }1 O0 [: U3 X  I  A. ~- v& H; C! ^0 \1 a
CODE:
0 o. @# E$ V6 q% H; m* L8 Y. l( N, g. E, u* S  `) ]
3 w2 U. I& Y/ p
drop table tmp;8 A! A4 ~& h" Z' b  _. ]
create table tmp
9 {8 k; `$ [  }/ S+ f7 O2 V0 p; J2 C(
. F! i( ~; o! X+ L' M[id] [int] IDENTITY (1,1) NOT NULL,& @9 ^% k: D, Q+ J9 ?
[name] [nvarchar] (300) NOT NULL,. I5 e# `$ \* e2 k& Z
[depth] [int] NOT NULL
8 a* V! u# l5 b0 A& c! h" ~4 r);
) P1 B6 J6 e3 {2 l' {
9 E$ N* Q6 C6 Bdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
. k' |1 O# F3 B, V# f% y! W) Hset @root='f:\usr\' -- Start root+ j* _2 M0 m' g# r
set @name='donggeer' -- directory to find
2 G3 A5 y. U8 Winsert into tmp exec master..xp_dirtree @root,0,0
0 W( ~) g  t0 o6 W0 W: `set @id=(select top 1 id from tmp where name=@name) $ V% j1 z) u; ^% H, Z
set @depth=(select top 1 depth from tmp where name=@name)
* |7 ^( a1 `+ P! Jwhile @depth<>1 ( H/ _$ H9 @' W8 G7 l2 V; ]
begin % t" X( O! l' E+ M0 ^5 o! @* G! {
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)7 a9 R) G% H, ]$ z* z
set @depth=(select depth from tmp where id=@id) 4 W, S# A7 l! D' r! V. |+ l& O
set @name=(select name from tmp where id=@id)+'\'+@name
! @8 _! w1 v% {1 j0 M: s! t4 cend update tmp set name=@root+@name where id=1
) ]+ s. d, D$ ^select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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