我有一个存储过程,它通过xml输入在我的2008 SQL服务器表中插入新记录:
CREATE PROCEDURE ins_AddBinsToBox
@BoxId BIGINT,
@BinIds XML
AS
BEGIN
INSERT INTO WebServiceBoxDetails
(
BinId,
BoxId
)
SELECT
ParamValues.ID.value('.','VARCHAR(20)'),
@BoxId
FROM
@binIds.nodes('/Bins/id') AS ParamValues(ID)
这非常适合插入新行,我很困惑的是更新(通过UPDATE语句)这个表与新的xml输入?
表:
Id(PK) BoxNumber BinId
(bigint) (bigint) (int)
_______________________
1 12 334
2 12 445
3 12 776
4 16 223
5 16 669
要使用的命令:
EXEC upd_Box @binIds='<Bins><id>7848</id><id>76554</id><id>71875</id></Bins>', @BoxId=12
答案 0 :(得分:1)
您可以再次使用NODES方法。
UPDATE WebServiceBoxDetails
SET BinID = ParamValues.ID.value('@BinID','VARCHAR(20)')
FROM
@bindIDs.nodes('/Bins/id') AS ParamValues(ID)
JOIN WebServiceBoxDetails w ON w.ID = ParamValues.ID.value('@id','VARCHAR(20)')
WHERE w.BoxNumber = @BoxID
答案 1 :(得分:1)
试试这个
DECLARE @binIds AS XML = '<Bins><id>7848</id><id>76554</id><id>71875</id></Bins>'
DECLARE @id INT
DECLARE @newid INT
DECLARE @count INT = 1
DECLARE @BinIdTable TABLE(RowNumber INT, BinId INT)
INSERT @BinIdTable(RowNumber, BinId)
SELECT ROW_NUMBER() OVER(ORDER BY ID), ParamValues.ID.value('.','INT')
FROM
@binIds.nodes('/Bins/id') AS ParamValues(ID)
DECLARE MyCursor CURSOR FOR
SELECT id FROM WebServiceBoxDetails WHERE BoxNumber = 12 ORDER BY id
OPEN MyCursor
FETCH NEXT FROM MyCursor
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @newid = B.BinId
FROM @BinIdTable B
WHERE RowNumber = @count
UPDATE WebServiceBoxDetails
SET BinId = @newid
WHERE Id = @id
SET @count = @count + 1
FETCH NEXT FROM MyCursor
INTO @id
END
CLOSE MyCursor
DEALLOCATE MyCursor
让我知道它是怎么回事。
答案 2 :(得分:1)
抱歉延误。以下是第二部分的答案(这应该适用于更新和插入)
DECLARE @binIds AS XML = '<Bins><id>44</id><id>55</id><id>66</id><id>77</id></Bins>'
DECLARE @id INT
DECLARE @newid INT
DECLARE @count INT = 1
DECLARE @BinIdTable TABLE(RowNumber INT, BinId INT)
DECLARE @BoxNumber INT = 12
DECLARE @RowCount INT = 0
INSERT @BinIdTable(RowNumber, BinId)
SELECT ROW_NUMBER() OVER(ORDER BY ID), ParamValues.ID.value('.','INT')
FROM
@binIds.nodes('/Bins/id') AS ParamValues(ID)
SELECT @RowCount = COUNT(*) FROM @BinIdTable
DECLARE MyCursor CURSOR FOR
SELECT id FROM WebServiceBoxDetails WHERE BoxNumber = @BoxNumber ORDER BY id
OPEN MyCursor
FETCH NEXT FROM MyCursor
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @newid = B.BinId
FROM @BinIdTable B
WHERE RowNumber = @count
UPDATE WebServiceBoxDetails
SET BinId = @newid
WHERE Id = @id
SET @count = @count + 1
FETCH NEXT FROM MyCursor
INTO @id
END
CLOSE MyCursor
DEALLOCATE MyCursor
IF @RowCount >= @count
BEGIN
INSERT INTO WebServiceBoxDetails
SELECT @BoxNumber, BinId
FROM @BinIdTable
WHERE RowNumber >= @count
END
让我知道它是怎么回事。