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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句
2 i) N6 p) c' `* R3 C3 A* O* W/ M' r% V; O! E  N1 d6 |* p% P
CODE:) |$ P: }7 J# s- S; E; ~
4 I: n7 ]( R' p+ C  E
drop table tmp;
9 v, J  }0 R: J' rcreate table tmp% L+ h$ {; G- `! D3 L# e8 [
(. H- {+ m7 T; T2 f2 D7 v: y! b
[id] [int] IDENTITY (1,1) NOT NULL," _0 W5 D5 t+ s5 X; O
[name] [nvarchar] (300) NOT NULL,! \0 e. W8 b2 Z
[depth] [int] NOT NULL,
: w7 w7 u8 J) O  U9 C" I4 z[isfile] [nvarchar] (50) NULL
* K; O* i) `4 j* x( @$ S* q2 P);
8 |! R" e- `) y) P! y: X1 j8 y% Y1 ^# e
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)6 ?9 R6 X1 p% P/ U3 g/ T
set @root='f:\usr\' -- Start root5 \- K. T6 N, Q# u' T" \4 J
set @name='cmd.exe'   -- Find file0 x+ a; u0 h5 R( @5 Z" ?. d7 z& c
insert into tmp exec master..xp_dirtree @root,0,1--4 P8 |4 b6 [+ v. m; a; s
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
3 v8 l: Z# o& K7 i7 E% Mset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
9 Z" ?# T, [% Vwhile @depth<>1 - t% z  ]! S% N
begin
7 J# j4 Z2 i" L6 Gset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 9 C3 B6 @* c- W+ g* Q. @
set @depth=(select depth from tmp where id=@id) / j! D6 u$ S9 |2 A
set @name=(select name from tmp where id=@id)+'\'+@name
! D8 F) l0 K; U7 Oend
& `. d6 X( C0 g( K+ ?* y% k, y/ I4 ?( qupdate tmp set name=@root+@name where id=1
' `" W! N  @# Y  Dselect name from tmp where id=1
% o' [( j# c# K2 g) \4 o5 e. s+ y7 o" j9 n
查找目录的语句$ p, N3 B9 o1 L6 S0 v+ y1 P
! Y  {) P, N; y1 p/ M3 M' v6 ]
! B+ S9 F; s+ E2 g7 Z
CODE:
0 e, \2 R5 E& c: ?" Z4 P8 R& c/ V/ s( [$ O/ n
' L( k2 @7 ~0 A4 q
drop table tmp;/ q% z4 v$ q" t
create table tmp1 F* b, i2 A6 k4 q( v+ x
(7 Z$ _9 m3 v4 G
[id] [int] IDENTITY (1,1) NOT NULL,
+ k0 C- M3 n) l7 e7 `% Y* Y  W[name] [nvarchar] (300) NOT NULL,
: v5 j& p6 ~9 B) u  }0 o: x[depth] [int] NOT NULL
; s9 C9 T6 A6 x. Y$ X6 N' q);9 k- ?# `, i( F: [5 g
# w$ W+ t* \, z9 N# m
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
1 i2 R% |6 U& y' w! W% Sset @root='f:\usr\' -- Start root8 B% q5 K2 |1 w4 s- x& I& D
set @name='donggeer' -- directory to find+ j; d3 P! u1 g6 n
insert into tmp exec master..xp_dirtree @root,0,0
+ L; b6 W  i( H  n. r# y: K: eset @id=(select top 1 id from tmp where name=@name) + m3 b) p7 w; I4 N
set @depth=(select top 1 depth from tmp where name=@name)
% e3 ?' n# M, q% ^2 j& x( s& O7 {while @depth<>1 - ^. k, w, H, ~& F& P1 y
begin 3 o* ]' P! B: B" t: C2 f" ~3 n7 ~
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
6 `4 r2 Z) [! |! `0 A( bset @depth=(select depth from tmp where id=@id)
" c9 @1 }6 N: [8 i6 s6 P9 q- zset @name=(select name from tmp where id=@id)+'\'+@name $ L- e5 F. y/ l/ l. v* D7 [
end update tmp set name=@root+@name where id=1. u- e4 N- z; b; h+ m5 p3 K- W
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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