我有一个Stored Proc,它将一些数据作为XML获取。我配置了一个SQL作业来触发cmd shell bcp实用程序输出到xml文件。当我运行sql作业时,我收到以下错误:
SQLState = 37000, NativeError = 5302 Error = [Microsoft][SQL Native Client][SQL Server]Mutator 'modify()' on '@XMLData' cannot be called on a null value. Process Exit Code 1. The step failed.
这是导致上述错误的SP中的行。
SET @XMLData.modify('insert <NoOfItems>{ xs:string(sql:variable("@NoOfItems")) }</NoOfItems> as first into (/Item)[1]')
但是,我非常确定它不是空的。我甚至在触发修改调用之前检查它是否为null。此外,如果我直接在SSMS中运行存储过程,它运行正常。 bcp中有一些东西我不明白为什么会导致这种情况。
以下是存储过程:
DECLARE @XMLData XML
DECLARE @NoOfItems int
SET @NoOfItems = (Select COUNT(*) from ItemInfo)
If (@NoOfItems > 0)
BEGIN
SET @XMLData =
(
Select
ItemNo,
(
select
Name,
Contents
from ItemInfo rsItemContent
where rsItemContent.ItemNo = rsItems.ItemNo
for xml path('Content'), type
)
from (select distinct ItemNo from IELSBE_V12.dbo.GDInfo) rsItems join ItemInfo II on rsItems.ItemNo = II.ItemNo
FOR XML PATH ('ItemInfo'), root ('Item')
)
if @XMLData is not null
BEGIN
SET @XMLData.modify('insert <NoOfItems>{ xs:string(sql:variable("@NoOfItems")) }</NoOfItems> as first into (/Item)[1]')
select @XMLData
END
END