我创建了一个存储过程,试图通过该存储过程从xml插入数据。数据未在db中插入或更新。我无法 解决了为什么未插入数据的问题。所以请看一下我具有表结构和SP主体的代码,并指导我将代码更改为 正确运行我的SP。
CREATE TABLE [dbo].[TickerBrokerStandardDateLineitemValue] (
[TabName] VARCHAR (MAX) NULL,
[StandardDate] VARCHAR (MAX) NULL,
[XFundCode] VARCHAR (MAX) NULL,
[BRTab] VARCHAR (MAX) NULL,
[BRLineItem] VARCHAR (MAX) NULL,
[StandardLineItem] VARCHAR (MAX) NULL,
[StandardValue] DECIMAL (18, 2) NULL,
[ActualProvidedByCompany] VARCHAR (MAX) NULL,
[ID] INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_TickerBrokerStandardDateLineitemValue] PRIMARY KEY CLUSTERED ([ID] ASC)
);
CREATE PROCEDURE [dbo].[uspInsertBoggyXmlData]
(
@inputxml VARCHAR,
@Status INT OUTPUT
)
As
Begin
Declare @intCntr as INT
Declare @intError as INT
Declare @XMLFormat as INT
Declare @TabName as Varchar(MAX)
Declare @StandardDate as Varchar(MAX)
Declare @XFundCode as Varchar(MAX)
Declare @BRTab as Varchar(MAX)
Declare @BRLineItem as Varchar(MAX)
Declare @StandardLineItem as Varchar(MAX)
Declare @StandardValue as Varchar(MAX)
Declare @ActualProvidedByCompany as Varchar(MAX)
Exec sp_xml_preparedocument @XMLFormat OUTPUT, @inputxml
-- Create Cursor from XML Table
Declare CurRecord
Cursor For
Select TabName,StandardDate,XFundCode,BRTab,BRLineItem,StandardLineItem,StandardValue,ActualProvidedByCompany
From Openxml (@XMLFormat, '/TickerBrokerStandardDateLineitem/TickerBrokerStandardDateLineitemValues/TickerBrokerStandardDateLineitemValue', 3)
With (
TabName Varchar(MAX),
StandardDate Varchar(MAX),
XFundCode Varchar(MAX),
BRTab Varchar(MAX),
BRLineItem Varchar(MAX),
StandardLineItem Varchar(MAX),
StandardValue Varchar(MAX),
ActualProvidedByCompany Varchar(MAX)
)
BEGIN TRANSACTION
SET @Status=0
SET @intCntr =0
Open CurRecord
Fetch Next From CurRecord Into @TabName,@StandardDate,@XFundCode,@BRTab,@BRLineItem,@StandardLineItem,@StandardValue,@ActualProvidedByCompany
While (@@Fetch_Status=0)
Begin
IF(@StandardValue='')
BEGIN
SET @StandardValue = 0
END
SELECT @intCntr = COUNT(*) FROM TickerBrokerStandardDateLineitemValue
WHERE TabName=@TabName AND StandardDate=@StandardDate AND StandardLineItem=@StandardLineItem
IF(@intCntr=0)
BEGIN
INSERT INTO TickerBrokerStandardDateLineitemValue (TabName,StandardDate,XFundCode,BRTab,BRLineItem,StandardLineItem,StandardValue,ActualProvidedByCompany)
VALUES(@TabName,@StandardDate,@XFundCode,@BRTab,@BRLineItem,@StandardLineItem,CAST(@StandardValue AS DECIMAL),@ActualProvidedByCompany)
END
ELSE
BEGIN
UPDATE TickerBrokerStandardDateLineitemValue
SET XFundCode = @XFundCode,
BRTab = @BRTab,
BRLineItem = @BRLineItem,
StandardValue = CAST(@StandardValue AS DECIMAL),
ActualProvidedByCompany = @ActualProvidedByCompany
WHERE TabName=@TabName AND StandardDate=@StandardDate AND StandardLineItem=@StandardLineItem
END
SELECT @intError = @@error if @intError <> 0 Begin ROLLBACK TRANSACTION return @intError End
Fetch Next From CurRecord Into @TabName,@StandardDate,@XFundCode,@BRTab,@BRLineItem,@StandardLineItem,@StandardValue,@ActualProvidedByCompany
End
Close CurRecord
Deallocate CurRecord
select @intError = @@error if @intError <> 0 Begin ROLLBACK TRANSACTION return @intError End
SET @Status=1
COMMIT TRANSACTION
SELECT @Status
END
这样我叫SP
Declare @inputxml VARCHAR(MAX)
declare @Status int
set @inputxml='<?xml version="1.0" encoding="utf-8"?>
<TickerBrokerStandardDateLineitem>
<Ticker />
<TickerID />
<TickerBrokerStandardDateLineitemValues>
<TickerBrokerStandardDateLineitemValue>
<TabName>CM Model101</TabName>
<StandardDate>2010 FY</StandardDate>
<XFundCode>TRIN0001</XFundCode>
<BRTab></BRTab>
<BRLineItem></BRLineItem>
<StandardLineItem>Net Revenue</StandardLineItem>
<StandardValue>1608.7</StandardValue>
<ActualProvidedByCompany>NO</ActualProvidedByCompany>
</TickerBrokerStandardDateLineitemValue>
<TickerBrokerStandardDateLineitemValue>
<TabName>JP Bank</TabName>
<StandardDate>3Q 2018</StandardDate>
<XFundCode>RD_015</XFundCode>
<BRTab></BRTab>
<BRLineItem></BRLineItem>
<StandardLineItem>Days of Inventory</StandardLineItem>
<StandardValue></StandardValue>
<ActualProvidedByCompany>YES</ActualProvidedByCompany>
</TickerBrokerStandardDateLineitemValue>
<TickerBrokerStandardDateLineitemValue>
<TabName>ZZZZZZZZZZZ</TabName>
<StandardDate>3Q 2018</StandardDate>
<XFundCode>RD_015</XFundCode>
<BRTab></BRTab>
<BRLineItem>991</BRLineItem>
<StandardLineItem>Days of Inventory</StandardLineItem>
<StandardValue>77.30</StandardValue>
<ActualProvidedByCompany>YES</ActualProvidedByCompany>
</TickerBrokerStandardDateLineitemValue>
</TickerBrokerStandardDateLineitemValues>
</TickerBrokerStandardDateLineitem>'
EXEC [dbo].[uspInsertBoggyXmlData] @inputxml, @Status output
Select @Status
答案 0 :(得分:1)
您的第一个步骤可能是归档或不返回任何东西。您可以检查一下。
begin try
begin tran
Exec sp_xml_preparedocument @XMLFormat OUTPUT, @inputxml
commit
end try
begin catch
if @@trancount > 0 rollback tran
select ERROR_MESSAGE()
;throw
end catch
然后,当然,在其余代码之前,您可以检查@XMLFormat
以确保它不是NULL
类似地,对于每次迭代,我都会使用错误处理将您的游标尝试包装在自己的事务中。 Erland Sommarskog has blogged extensively处理错误,因此在此不再赘述。