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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
% X3 k% J6 ?! w, R  ]+ T* ^# ]( u9 q5 I
CODE:
* j8 a  s3 b% q* i7 a1 ?* i$ ?9 r, |: [/ E+ m
drop table tmp;
/ _' j6 D  ^$ w7 u6 Rcreate table tmp
+ [, n$ [+ s5 ^( H2 k(
6 Z5 {" U- ^" R; S/ d8 |[id] [int] IDENTITY (1,1) NOT NULL,
6 {* H, h) @9 U[name] [nvarchar] (300) NOT NULL,/ E' ]2 e% D; J' h$ w# p/ ]) j
[depth] [int] NOT NULL,, o( }% P  E3 T" \9 @' s* y
[isfile] [nvarchar] (50) NULL% y1 e  t2 m. j6 q; o
);0 K7 C- i7 z  h1 P' S& {
5 L0 t1 }# O; T3 ~8 j& M
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)7 r' ?% x$ f# E  _
set @root='f:\usr\' -- Start root
) I2 f7 x2 a+ R' Bset @name='cmd.exe'   -- Find file
  Q" a! H4 {7 A: K% ainsert into tmp exec master..xp_dirtree @root,0,1--
6 o5 q' P+ S' t0 y0 K5 Rset @id=(select top 1 id from tmp where isfile=1 and name=@name) % b; @" U* ^* b
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
' r9 Z0 A5 u* F- W9 }while @depth<>1 + U9 m" L6 |1 C% K& y& ?
begin ; n+ P8 N/ d; L- d7 K$ E
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ; d! E2 J/ t, S3 P* a2 r* r* p, T
set @depth=(select depth from tmp where id=@id)
5 t3 ?0 K0 r. oset @name=(select name from tmp where id=@id)+'\'+@name# p. N9 H6 s  R/ m9 r$ J7 M
end! h( s- U- Y" ?* ]7 u# K2 u
update tmp set name=@root+@name where id=1
$ i8 |7 z8 _8 O0 |" l0 q8 N. V& Wselect name from tmp where id=1
0 m4 i# I6 _  s
5 m3 M5 f1 S) M/ n# _查找目录的语句& z3 V2 K1 Z+ _' M3 C

4 u( T& b. }* m9 l, J1 p! [
/ |; u; ?7 M1 \. Q, ]CODE:( n9 v+ L* ~9 z' A, F/ T: J, r8 v
: Q/ e7 B; L! u+ N- O4 r8 G/ G
2 m( G# n* _: w5 o4 S$ V% `
drop table tmp;- S+ S3 h* s3 l2 l0 Q7 [7 B
create table tmp
; i: m5 I. w& u$ ~8 d0 ]- J- k8 X- y(
1 H3 Y0 r' ^' t. _6 Q' o[id] [int] IDENTITY (1,1) NOT NULL,
7 [2 [$ q: ?- `+ N5 Q[name] [nvarchar] (300) NOT NULL,# N1 C* h7 _$ H' e5 _2 G( {
[depth] [int] NOT NULL9 n) z' A5 g! S, H' Y: e9 u
);
/ B: I# m8 o3 J' M
" D1 \/ K) o" H- w) ldeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
9 z2 i+ v  k5 Qset @root='f:\usr\' -- Start root0 T$ @& g4 j! T, k! K' |
set @name='donggeer' -- directory to find! D/ V* s! K; D5 g: s: i( p
insert into tmp exec master..xp_dirtree @root,0,0- h+ ^0 y# E* t4 f+ |
set @id=(select top 1 id from tmp where name=@name) 8 D1 {' p  a* D' v* U
set @depth=(select top 1 depth from tmp where name=@name)
; a( B' ^6 U$ C0 u1 o7 ewhile @depth<>1
( G; O0 A3 |0 i) {! }4 V2 Q, L+ fbegin 8 j. Z1 W7 o* l' M
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc). g3 z4 Q) Q, I) k
set @depth=(select depth from tmp where id=@id) # j3 b/ e7 N5 l; G$ r
set @name=(select name from tmp where id=@id)+'\'+@name
" U& s& j" T4 c/ ?end update tmp set name=@root+@name where id=1
. z5 t6 P  o: J, G* ~5 {select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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