日期:2014-05-18  浏览次数:20458 次

【mm】求教大虾,mssql2005中如何检测文件是否存在????
2000里面有这个xp_fileexist可以检测,但貌似2005中木有了?
请问2005中怎么检测指定路径的文件是否存在呢?

SQL code

declare @num int --申明一个接受返回值的变量 YX_UpFile/Small/b0001072300.jpg
EXEC master..xp_fileexist 'I:\www.jingying.me_vss\YX_UpFile\Small\b0000920333.jpg',@num output -- 执行文件存在否的验证 存在返回1 不存在返回0
if(@num = 1) --如果存在就给出提示或做其他功能的实现
begin
    print '文件已经存在'
end
else --该文件不存在执行备份操作
begin
    print '文件不存在'
end




------解决方案--------------------
SQL code
declare @FileExist table(Col1 int,Col2 int,Col3 int)

insert @FileExist exec xp_fileexist @Path

select 1 from @FileExist where Col2=0--判斷

------解决方案--------------------
SQL code
  exec master..xp_cmdshell 'dir "I:\www.jingying.me_vss\YX_UpFile\Small\b0000920333.jpg"'

------解决方案--------------------
SQL code
create table #t(f nvarchar(100))
insert into #t
exec xp_cmdshell "dir I:\www.jingying.me_vss\YX_UpFile\Small\b0000920333.jpg"
if not exists(select 1 from #t where f='b0000920333.jpg'
print '文件存在'
else
print '文件不存在'

------解决方案--------------------
刚测试过, SQL 2008 R2亦有
SQL code

exec xp_fileexist 'C:\boot.ini'

File Exists File is a Directory Parent Directory Exists
----------- ------------------- -----------------------
0           0                   1

------解决方案--------------------
SQL code
--是否允许运行系统存储过程xp_cmdshell
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go
exec master..xp_cmdshell 'dir "I:\www.jingying.me_vss\YX_UpFile\Small\b0000920333.jpg"'
go
sp_configure 'show advanced options',0
reconfigure
go

/*配置选项 'show advanced options' 已从 1 更改为 1。请运行 RECONFIGURE 语句进行安装。
配置选项 'xp_cmdshell' 已从 1 更改为 1。请运行 RECONFIGURE 语句进行安装。
output
---------------------------------------------------------------------------------------------------------------
系统找不到指定的路径。
NULL

(2 行受影响)

配置选项 'show advanced options' 已从 1 更改为 0。请运行 RECONFIGURE 语句进行安装。
*/

------解决方案--------------------
SELECT * FROM master.dbo.sysobjects WHERE name='xp_fileexist'--查看是否存在
------解决方案--------------------
SQL code
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go
exec master..xp_cmdshell 'dir "D:\Program Files\WinRAR\Default.SFX"'
go
sp_configure 'show advanced options',0
reconfigure
go

/*配置选项 'show advanced options' 已从 0 更改为 1。请运行 RECONFIGURE 语句进行安装。
配置选项 'xp_cmdshell' 已从 1 更改为 1。请运行 RECONFIGURE 语句进行安装。
output
---------------------------------------------------------------------------------------------------------------
 驱动器 D 中的卷没有标签。
 卷的序列号是 9091-BCC1
NULL
 D:\Program Files\WinRAR 的目录
NULL
2011/05/31  09:55            96,256 Default.SFX
               1 个文件         96,256 字节
               0 个目录 36,912,943,104 可用字节
NULL

(9 行受影响)

配置选项 'show advanced options' 已从 1 更改为 0。请运行 RECONFIGURE 语句进行安装。
*/

------解决方案--------------------
探讨

引用:

SELECT * FROM master.dbo.sysobjects WHERE name='xp_fileexist'--查看是否存在