我正在尝试从文件夹中使用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:我已经从网上找到的代码中整理出来了。
答案 0 :(得分:0)
我认为您不能在OPENROWSET
命令中使用T-SQL变量 - 这些事情需要完整明确地说明。
如果需要在XML文件列表上执行此操作,则必须以字符串形式创建T-SQL命令,然后使用动态SQL执行该命令。