0

我正在尝试使用 OpenRowSet 从文件夹中读取 XML 文件,但无法这样做并收到错误消息

无法批量加载,因为“'@FullFilename'' 不存在。

如果有人能建议我如何纠正问题以从每个 XML 文件中获取所有数据,我将不胜感激。

谢谢。

代码:

declare @Directory varchar(50)
select @Directory = 'E:\XML\'

DECLARE @CD TABLE (XMLData XML);
declare @FileExist int
DECLARE @FileName varchar(500),
@DeleteCommand varchar(1000),
@FullFileName varchar(500)

DECLARE @SQL NVARCHAR(1000),@xml xml

--This is so that we know how long the loop lasts
declare @LoopID int, @MaxID int
SELECT @LoopID = min(id),@MaxID = max(ID)
FROM #tempList

WHILE @LoopID <= @MaxID
BEGIN
    SELECT @FileNAme = filename
    from #tempList
    where id = @LoopID

    SELECT @FullFileName = @Directory + @FileName 
    print @FULLFileName

    exec xp_fileexist @FullFileName , @FileExist output

    if @FileExist =1 --sanity check in case some evil person removed the file
    begin

---********************************Problem with @FullFileName----------------     
    INSERT INTO @CD
    SELECT *
    FROM OPENROWSET(BULK ''' + @FullFileName +''' ,Single_BLOB) as rs
---********************************------------

    select * from @CD

    --SET @DeleteCommand = 'del ' +  @Directory + @FileName 
    --maybe you want to delete or move the file to another directory
    -- ** here is how to delete the files you just imported
    -- uncomment line below to delete the file just inserted
    --EXEC MASTER..XP_CMDSHELL @DeleteCommand
    -- ** end of here is how to delete the files
    end

    --Get the next id, instead of +1 we grab the next value in case of skipped id values
    SELECT @LoopID = min(id)
    FROM #tempList
    where id > @LoopID
END

select * from #tempList

这有效,我能够从指定文件中获取 XML 数据

DECLARE @CD TABLE (XMLData XML);
Declare @get_GeneralID bigint
INSERT INTO @CD
SELECT *
FROM OPENROWSET(BULK N'E:\XML\TestResult.XML', SINGLE_BLOB) rs;
select * from @CD

PS:我整理了从网上找到的代码。

4

1 回答 1

0

我不认为你可以在你的OPENROWSET命令中使用 T-SQL 变量——这些东西需要完全明确地说明。

如果您需要对 XML 文件列表执行此操作,则必须将 T-SQL 命令创建为字符串,然后使用动态 SQL 执行该命令。

于 2013-03-12T14:01:37.487 回答