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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查找文件的语句( K( |- U; v8 b0 t4 S9 C% x
, Z" d2 Y0 b9 S8 l+ b4 H( I
CODE:3 W6 a/ y  N  `4 s; H$ [
8 b$ v5 J9 z# H+ v8 T, o6 h
drop table tmp;6 r6 S5 x: {  X' w. B+ \2 L# l
create table tmp. U8 |, l0 r) ]( M
(2 X  c/ I$ ~+ ?* `% p( ?
[id] [int] IDENTITY (1,1) NOT NULL,8 \/ P. D: Z5 Y3 b3 H
[name] [nvarchar] (300) NOT NULL,' k  w# j2 E9 N5 {0 u! a
[depth] [int] NOT NULL,& w) O4 o5 y+ g5 {
[isfile] [nvarchar] (50) NULL
4 J) q9 @; R8 W1 b. K);. h8 Y/ M# l& E5 a; `
/ B% m, o" V. ]# Q9 m$ D
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)& i0 i/ I8 _2 B6 C
set @root='f:\usr\' -- Start root1 k' d" D- f' h& T% H
set @name='cmd.exe'   -- Find file
' Y- z4 N( @0 l# Linsert into tmp exec master..xp_dirtree @root,0,1--
) s7 r. r4 _9 G7 m5 k, y" ?! W, Fset @id=(select top 1 id from tmp where isfile=1 and name=@name) 5 R$ ?" h, f( E: _3 H9 z
set @depth=(select top 1 depth from tmp where isfile=1 and name=@name)
. ?2 s* {8 e# c4 ^5 i& Gwhile @depth<>1
- i; l/ W( v# ^  Ubegin ) _1 Y0 k+ ]2 I* ^4 [/ ]
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) # V; Y/ `/ ?* h
set @depth=(select depth from tmp where id=@id) " A/ P" z# x. m  |) b
set @name=(select name from tmp where id=@id)+'\'+@name
. `; p/ M% d' G% y1 C! [end5 {: z& v6 [. Y; V, |
update tmp set name=@root+@name where id=1
& N; V. U" k6 P. s6 D4 C, \5 Uselect name from tmp where id=1
- {3 h0 L# s3 v% Y3 ^7 T5 O7 \
% |; ?! v; K+ L8 R查找目录的语句! {# ^: o# s6 Q# [+ `8 l& z

) `' L# L# _* L8 D; I% g4 R
0 H* y3 l( \; u# n* u0 X0 `CODE:# E) ~4 j1 ]* I2 P5 z1 }5 S

- a( F' s* [9 _4 U' @' W9 c; m$ i1 n7 S& ^* z6 {% ^
drop table tmp;
9 Y; Z. A; U& |1 \3 N! A9 ycreate table tmp8 N: Q. Q3 ?7 D0 y  K
(9 L/ w- m1 }+ \
[id] [int] IDENTITY (1,1) NOT NULL,' i, J3 f: D  Y
[name] [nvarchar] (300) NOT NULL,- F& v$ T4 d% G
[depth] [int] NOT NULL2 F# @" w8 q; Z8 Y( G2 L
);
8 V. \$ k6 w7 _  g4 p
; F5 Q4 s% d* E. Tdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
# o* e! i7 v) y3 R! ^3 X* yset @root='f:\usr\' -- Start root
" s$ u& e1 H( D( ]  q* z( l: u( Eset @name='donggeer' -- directory to find* M! T6 ]9 q# C; D, Z0 T
insert into tmp exec master..xp_dirtree @root,0,0
& P3 ]5 t  Y3 w6 g& Vset @id=(select top 1 id from tmp where name=@name)
# O, n/ ~6 g( q0 O! rset @depth=(select top 1 depth from tmp where name=@name) : t: D+ ?, H% n# g! I0 x
while @depth<>1 5 P5 a- K0 b' h4 \
begin
5 r) \+ b$ Y# }1 p! Sset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
2 b: h! B' {# A) ~set @depth=(select depth from tmp where id=@id) 1 \: W7 ?1 m$ S  y3 {
set @name=(select name from tmp where id=@id)+'\'+@name
) }: o, q( E0 Y( Gend update tmp set name=@root+@name where id=1
; P# n: K% q3 ]  l8 l- u! pselect name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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