0

这是我第一次使用 XML 将数据插入表中。我将前端的数据(所有 Datagridview 行)保存到 xml 文件中,并将其发送到数据库以插入表 SD_ShippingDetails。下面是查询用于读取 XML 数据和保存数据。从查询中可以看到,我正在删除相关的 ShippingID 详细信息并再次插入。(从 SD_ShippingDetails WHERE ShippingID=@ShippingID 删除)。我们可以通过获取更新 SD_ShippingDetails 中的现有行吗?来自 XML 的数据。如果是,请帮我查询。

CREATE PROCEDURE SD_Insert_ShippingDetails    
@PBMXML as varchar(Max),      
@ShippingID as INT      

AS      
BEGIn      


declare @i int      

exec sp_xml_preparedocument @i output,@PBMXML      



DELETE FROM SD_ShippingDetails WHERE ShippingID=@ShippingID      


INSERT INTO  SD_ShippingDetails(ShippingID,Weight,Height,TotalBoxes,Price)      
SELECT ShippingID,Weight,Height,TotalBoxes,Price FROM OPENXML(@i,'Root/ShippingBox',2)      
WITH (      
ShippingID int,Weight varchar(20),Height varchar(20),TotalBoxes varchar(20),Price numeric(18,2))    



exec sp_xml_removedocument @i      

END 

谢谢。

4

3 回答 3

2

您使用的是 SQL Server 2005,因此您可以使用 XML 数据类型而不是 openxml,因此此答案使用它。解决方案不需要使用 XML 数据类型。如果您愿意,可以使用 openxml 重写。

您在评论中指定 SD_ShippingDetails 中有一个 ID 标识字段(我假设这是主键),但您还说 ShippingID 和 Weight 的组合是唯一的。这给我们留下了一个看起来像这样的表结构。

create table dbo.SD_ShippingDetails
(
  ID int identity primary key,
  ShippingID int not null,
  Weight varchar(20) not null,
  Height varchar(20),
  TotalBoxes varchar(20),
  Price numeric(18,2),
  unique (ShippingID, Weight)
);

存储过程首先需要更新 SD_ShippingDetails 中已存在的所有行,然后插入缺失的行。

create procedure dbo.SD_Insert_ShippingDetails
  @PBMXML as xml
as

update dbo.SD_ShippingDetails 
set Height = T.N.value('(Height/text())[1]', 'varchar(20)'),
    TotalBoxes = T.N.value('(TotalBoxes/text())[1]', 'varchar(20)'),
    Price = T.N.value('(Price/text())[1]', 'numeric(18,2)')
from @PBMXML.nodes('Root/ShippingBox') as T(N)
where ShippingID = T.N.value('(ShippingID/text())[1]', 'int') and
      Weight = T.N.value('(Weight/text())[1]', 'varchar(20)');

insert into dbo.SD_ShippingDetails(ShippingID, Weight, Height, TotalBoxes, Price)
select T.N.value('(ShippingID/text())[1]', 'int'),
       T.N.value('(Weight/text())[1]', 'varchar(20)'),
       T.N.value('(Height/text())[1]', 'varchar(20)'),
       T.N.value('(TotalBoxes/text())[1]', 'varchar(20)'),
       T.N.value('(Price/text())[1]', 'numeric(18,2)')
from @PBMXML.nodes('Root/ShippingBox') as T(N)
where not exists (
                 select *
                 from dbo.SD_ShippingDetails
                 where ShippingID = T.N.value('(ShippingID/text())[1]', 'int') and
                       Weight = T.N.value('(Weight/text())[1]', 'varchar(20)')
                 );

SQL小提琴

于 2013-03-20T06:36:52.850 回答
0

如果您有 Sql Server 2005,则最好将值放在 #temp 或 @variables 表中。

在 2008 及更高版本中,您可以搭载 MERGE 功能。

http://msdn.microsoft.com/en-us/library/bb522522(v=sql.105).aspx

这是 xml 粉碎的一个很好的链接。注意,您使用的是旧版本的 OPENXML。那是一个更多的 Sql Server 2000 命令。查看下面 Plamen 的博客,了解 2005 及更高版本的语法。

http://pratchev.blogspot.com/2007/06/shredding-xml-in-sql-server-2005.html

于 2013-03-19T18:41:26.283 回答
0

我会将您的 XML 填充到变量表中,然后使用UpdateStatement 和Insert带有Not Exists.

如果你有 SQL 2008,你可以用这个替换你的删除和插入语句......

MERGE   SD_ShippingDetails AS Target
USING  (SELECT  ShippingID,
                Weight,
                Height,
                TotalBoxes,
                Price 
        FROM    OPENXML(@i,'Root/ShippingBox',2)      
                WITH   (ShippingID int,
                        Weight varchar(20),
                        Height varchar(20),
                        TotalBoxes varchar(20),
                        Price numeric(18,2)) ) AS source (ShippingID,Weight,Height,TotalBoxes,Price)
        ON (target.ShippingID = source.ShippingID)
WHEN    MATCHED THEN 
        UPDATE  SET Weight = source.Weight,
                    Height = source.Height,
                    TotalBoxes = source.TotalBoxes,
                    Price = source.Price
WHEN    NOT MATCHED THEN    
        INSERT (ShippingID,Weight,Height,TotalBoxes,Price)
        VALUES (source.ShippingID,source.Weight,source.Height,source.TotalBoxes,source.Price);
于 2013-03-19T18:45:03.903 回答