0

想象一下,我有一个名为 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 时,记录将从表中删除。问题解决了。

4

1 回答 1

1

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

但是,您可以在 a 中循环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
于 2010-10-25T10:53:23.123 回答