当前位置: 代码迷 >> Sql Server >> 【mm】求教,mssql2005中怎么检测文件是否存在?
  详细解决方案

【mm】求教,mssql2005中怎么检测文件是否存在?

热度:41   发布时间:2016-04-27 14:43:14.0
【mm】求教大虾,mssql2005中如何检测文件是否存在????
2000里面有这个xp_fileexist可以检测,但貌似2005中木有了?
请问2005中怎么检测指定路径的文件是否存在呢?

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



------解决方案--------------------
SQL code
declare @FileExist table(Col1 int,Col2 int,Col3 int)insert @FileExist exec xp_fileexist @Pathselect 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 #texec 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 '文件存在'elseprint '文件不存在'
------解决方案--------------------
刚测试过, 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_cmdshellsp_configure 'show advanced options',1reconfiguregosp_configure 'xp_cmdshell',1reconfiguregoexec master..xp_cmdshell 'dir "I:\www.jingying.me_vss\YX_UpFile\Small\b0000920333.jpg"'gosp_configure 'show advanced options',0reconfigurego/*配置选项 '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',1reconfiguregosp_configure 'xp_cmdshell',1reconfiguregoexec master..xp_cmdshell 'dir "D:\Program Files\WinRAR\Default.SFX"'gosp_configure 'show advanced options',0reconfigurego/*配置选项 'show advanced options' 已从 0 更改为 1。请运行 RECONFIGURE 语句进行安装。配置选项 'xp_cmdshell' 已从 1 更改为 1。请运行 RECONFIGURE 语句进行安装。output--------------------------------------------------------------------------------------------------------------- 驱动器 D 中的卷没有标签。 卷的序列号是 9091-BCC1NULL D:\Program Files\WinRAR 的目录NULL2011/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'--查看是否存在