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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:42:56 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
查找文件的语句& Q. B4 c# X9 V+ F2 x, C

+ F. B2 l4 w$ E1 p4 n3 _CODE:( @1 A( N' O% u: `

* `5 U2 D' D) U9 }% S3 f5 t; x( t/ Idrop table tmp;
8 R9 n' i: O2 A& v4 Wcreate table tmp
/ Y) a& L3 b3 }6 [(
4 x$ ?; J; t# b[id] [int] IDENTITY (1,1) NOT NULL,/ `! Q; I$ N3 Q6 d
[name] [nvarchar] (300) NOT NULL,4 _6 M0 u6 ]" F4 T+ H
[depth] [int] NOT NULL,
& q2 W3 I4 {* w1 P5 G& ^/ S[isfile] [nvarchar] (50) NULL
' f4 f8 \. O$ R  V& g( c/ O);7 O9 S5 `, e  P# i! d

: I3 K7 ?8 v  T1 L# Qdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)8 C' B% H% @: \; ?
set @root='f:\usr\' -- Start root8 f" a& {( z( N" {* h& T
set @name='cmd.exe'   -- Find file
$ \" i) ?) _/ U$ p9 L% ainsert into tmp exec master..xp_dirtree @root,0,1--6 F3 A* J) h+ w2 ]! t8 L- P
set @id=(select top 1 id from tmp where isfile=1 and name=@name)
6 `- M  X; x+ m  @9 wset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)6 s0 F, O. z, G3 u# K/ [9 I* y
while @depth<>1 & T& K1 E* `% r* {1 Z4 @
begin
* P. n9 F& @( R* r0 ]0 O/ K# Uset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) * Y4 i4 V. h! W2 x0 x1 Y! R
set @depth=(select depth from tmp where id=@id)
7 N5 M5 }! M, S$ E1 d( uset @name=(select name from tmp where id=@id)+'\'+@name+ o1 a4 U3 N5 j# g3 @
end
2 T3 {  k1 X/ nupdate tmp set name=@root+@name where id=16 y2 X$ F4 m2 }. _
select name from tmp where id=15 S0 E6 O3 ?# E; }
, ~) }( P5 T# q( H
查找目录的语句
7 K( }: g- `( k9 E" r' Q. D9 R0 g; H6 w6 N- U

% q4 g+ f# O, n# T2 o  j0 {CODE:
. i% W  |# G. h' _1 z
. |8 W( A$ V* Q0 G
$ M( z- E- N; ~$ Idrop table tmp;$ F$ u* L+ d; S; X. I7 M- I9 d& G) a
create table tmp
3 N' p3 ^1 x2 a' h(
  v: v# o6 c/ g9 f[id] [int] IDENTITY (1,1) NOT NULL,
) o  `9 Z# H1 r  _, O, ?[name] [nvarchar] (300) NOT NULL,- Z; p( `; Y+ \3 A! {
[depth] [int] NOT NULL
5 n# [7 J: n/ y2 X);
- s, u2 h; C, g2 ^8 y
0 a% I+ a* F7 G: |declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)- ]4 [' @- ^( e$ u$ C+ j
set @root='f:\usr\' -- Start root" y6 T1 \+ I4 C
set @name='donggeer' -- directory to find
! t* a8 s' o* @7 ?6 n/ n7 @6 pinsert into tmp exec master..xp_dirtree @root,0,0
$ B0 c- @5 a+ z' Pset @id=(select top 1 id from tmp where name=@name) ! Y. X* t, q0 G- r) w8 P$ f: n
set @depth=(select top 1 depth from tmp where name=@name) ( q/ T1 T% X& s, n
while @depth<>1 8 f' v( A: U( C' U5 S* [+ M
begin
3 m/ R$ i5 c, u. l7 E# c5 Hset @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
+ w, G% r2 Y0 M) |$ dset @depth=(select depth from tmp where id=@id)   p/ E: v0 k8 {; F; `0 o) \( h
set @name=(select name from tmp where id=@id)+'\'+@name
: l7 C; N2 U/ s& ^( Z$ X0 T: U, rend update tmp set name=@root+@name where id=17 X* |2 t) y: x
select name from tmp where id=1
回复

使用道具 举报

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

本版积分规则

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