在我的一个 c# 项目中,我必须在数据库表中存储多行。为此,我在存储过程中将 Xml 数据作为字符串数据类型传递,然后我改回 XML,然后使用 sp_xml_prepareddocument 存储到表中。
我想知道这是否是正确的方法。或者是否有任何其他最佳方法可用于处理这种情况。当 XML 变大时是否有任何性能损失。
ALTER PROCEDURE [dbo].[InsertData]
@nSiteId int,
@nClaimId int,
@nEditSourceId int,
@sClaimEditXml NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON
DECLARE @hDoc INT, @nRC INT
EXEC @nRC = sp_xml_preparedocument @hDoc OUTPUT, @sClaimEditXml
IF @nRC <> 0
BEGIN
RAISERROR( N'apiInsertClaimCurrentEdits #1', 16, -1 )
RETURN -1
END
IF EXISTS(SELECT * from ClaimCurrentEdits where SiteID=@nSiteId and ClaimID=@nClaimId and EditSourceId = @nEditSourceId)
BEGIN
DELETE FROM ClaimCurrentEdits where SiteID=@nSiteId and ClaimID=@nClaimId and EditSourceId = @nEditSourceId
END
INSERT INTO [ClaimCurrentEdits]
([SiteID]
,[ClaimID]
,[ErrorID]
,[ErrorDesc]
,[Severity]
,[ValidationDate]
,[EditSourceId]
)
SELECT * FROM OPENXML( @hDoc, '/ValidationResults/ValidationResult',2 )
WITH
( SiteId int '../@SiteId',
ClaimId int '../@ClaimId',
ErrorId int '@ErrorId',
ErrorDesc varchar(2048) '@ErrorDesc',
Severity int '@Severity',
ValidationDate datetime '@ValidationDate',
EditSourceId int '@EditSourceId'
)
EXEC sp_xml_removedocument @hDoc
END
RETURN 0
/******************************************************************************/
GO