SQL 2000:只选择xp_fileexist = true的记录?

时间:2010-10-25 10:18:57

标签: sql file-exists

想象我有一个名为tbl_pictures的表:picture_id和picture_name,该表中有大约500条记录。图片存储在c:\ mypics \

问题:并非所有照片都存在,但它们仍在桌面上。 我怎样才能列出那些存在的图片?

我知道如何使用“EXEC Master.dbo.xp_fileexist @filename”检查单个文件是否存在,但我不知道如何在循环中执行此操作。

它应该如下所示:

SELECT picture_name FROM tbl_pictures WHERE (xp_fileexist '@picture_name' = true)

任何? :)


编辑:

我使用了一个asp.net循环,它调用了一个文件存在函数。 当它返回false时,记录将从表中删除。 问题解决了。

1 个答案:

答案 0 :(得分:1)

从存储过程中检索结果非常困难。基本上,您必须创建一个与其精确列输出匹配的表,并insert ... exec。在where条款中完全没有机会这样做。

然而,您可以在while中循环,然后将结果输入表中。例如:

set nocount on
if OBJECT_ID('tempdb..#TempFileList') is not null
    drop table #TempFileList
create table #TempFileList (file_name nvarchar(250), file_exist bit)
insert #TempFileList (file_name) values ('c:\windows\win.ini')
insert #TempFileList (file_name) values ('c:\somefile.txt')

if OBJECT_ID('tempdb..#TempFileResult') is not null
    drop table #TempFileResult
create table #TempFileResult (File_exists int, File_directory int,parent_dir int)


declare c cursor local fast_forward for select file_name from #TempFileList
open c
declare @file_name nvarchar(250)
fetch from c into @file_name
while @@FETCH_STATUS = 0
    begin
    delete from #TempFileResult
    insert into #TempFileResult exec Master.dbo.xp_fileexist @file_name

    update  #TempFileList
    set     file_exist = (select file_exists from #TempFileResult)
    where   file_name = @file_name

    fetch from c into @file_name
    end

close c
deallocate c

select * from #TempFileList

在我的系统上,打印:

file_name             file_exist
c:\windows\win.ini    1
c:\somefile.txt       0