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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句9 B  [4 r% s5 U! a( \

2 s/ z  j8 U& w% g/ j3 ?CODE:
6 [; r- S6 |9 }$ D+ C0 L1 |
8 n0 l. H3 W4 O; P8 ~. E! Q5 `. J: xdrop table tmp;6 {: j8 W- n- ?8 M: G& z
create table tmp( p# K6 m7 p! K! o( j3 {
(& ?+ K! Z  y# O1 q# |  @+ V
[id] [int] IDENTITY (1,1) NOT NULL,
' b( |1 a) G! ^' A* d[name] [nvarchar] (300) NOT NULL,2 z3 n- i# `. V5 o, B0 E
[depth] [int] NOT NULL,- l( F( K4 a" h0 M  c9 \# o
[isfile] [nvarchar] (50) NULL* S8 B) w* [2 ?* g  x$ Z9 M( t
);
8 ?. w6 g) n7 \) N6 [9 D% ^
0 R- t3 D# I' f2 |declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
3 w5 r: _" r7 O' c9 y( z( jset @root='f:\usr\' -- Start root
) }" n, M* O  c3 A7 Iset @name='cmd.exe'   -- Find file2 r  F9 c% J9 F8 C# j9 W
insert into tmp exec master..xp_dirtree @root,0,1--
7 X! ~5 [9 e/ ~# pset @id=(select top 1 id from tmp where isfile=1 and name=@name)
7 o1 q' z( @( lset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
/ Y2 ^: _" N) w' b+ ?! e2 K0 `0 B# W# wwhile @depth<>1 - B8 `8 S  Z3 Y7 f& p3 l
begin 6 P, _/ E2 L3 Q1 M
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
6 f1 }! V- [! s3 c) ~set @depth=(select depth from tmp where id=@id)
( f& r4 P! g. {" a. g- L3 dset @name=(select name from tmp where id=@id)+'\'+@name
. A+ N7 g, S& M8 y/ eend- Z/ h! ?; a/ ?& {/ z, Z6 V% k  a
update tmp set name=@root+@name where id=1, j# A' j: O0 W8 g0 O. m/ R3 f6 |
select name from tmp where id=1
% c. k  |( k  e8 H* M+ }* b. a4 H& ~+ \6 b4 k  ^
查找目录的语句" Z( O7 ]+ o; g3 B) B/ v- [

5 F: o0 m2 G$ k2 o8 {
0 g: D. _! [/ r& T8 Q% u$ wCODE:9 e- |5 D# I$ I* d
" M' ~1 c- g4 b0 \4 L4 T6 R, Q

+ s6 a( ~! S( V' j/ `drop table tmp;8 G3 j7 ~# C7 L: W) d5 q7 ~
create table tmp1 P9 n6 S' d5 ^; i. J
(
2 K4 Q" d1 W( n$ \. v7 m) J[id] [int] IDENTITY (1,1) NOT NULL,
/ U2 I) J4 z+ }% P# X" b: l& Z" e[name] [nvarchar] (300) NOT NULL,
( l6 W1 {2 c4 j! m  I' ~% A[depth] [int] NOT NULL
9 P0 Y" v( A) v1 I, a) T8 p; @$ T);
) M( u( R# D( p! |. l  l; X: T, B& }7 m+ J+ u: T, Q: r1 J
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)# q; @8 ]+ Q% d# |8 r) ?0 W/ U
set @root='f:\usr\' -- Start root# P; y, R8 Z/ {( d; l2 \
set @name='donggeer' -- directory to find" N, t* o0 x( a) i8 n3 C
insert into tmp exec master..xp_dirtree @root,0,0! P. E! G; `+ F" F* f
set @id=(select top 1 id from tmp where name=@name)
2 X& `( S  I$ l4 Kset @depth=(select top 1 depth from tmp where name=@name) 6 w  }4 u/ L. ?( u
while @depth<>1 ' B" m2 l8 s6 u1 ^
begin 6 u! Y' @) c* u+ Q
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc). E, @% B! l5 M4 V# [4 U9 [+ m. n
set @depth=(select depth from tmp where id=@id)
, U5 j6 J! j3 p+ |$ Sset @name=(select name from tmp where id=@id)+'\'+@name
, f$ C  z2 v5 d1 T4 M- ]end update tmp set name=@root+@name where id=1
( w: u$ d' r% M5 s8 L# |select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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