【发布时间】:2014-06-11 15:54:39
【问题描述】:
这是我第一次在 SQL 中使用 XML 输入。
我创建了以下过程,将我的 XML 字符串中的所有记录插入到我的表中,到目前为止效果很好。
有人可以告诉我如何更改它,以便它只插入一条新记录,如果itemID(我的 XML 中的每条记录也有这个)在我的表中尚不存在,列 itemID - 否则它应该使用 XML 中的新数据更新现有记录。
我一般都知道如何使用IF NOT EXISTS 和UPDATE,但不确定如何使用XML 字符串作为输入来实现这一点。
我的程序(到目前为止):
ALTER PROCEDURE [dbo].[editor_UpdateQuestions]
@xml xml
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO editor_Questions
(
categoryID,
question,
sortID,
modDate,
modBy
)
SELECT ParamValues.x1.value('categoryID[1]', 'int'),
ParamValues.x1.value('question[1]', 'nvarchar(1000)'),
ParamValues.x1.value('sortID[1]', 'int'),
GETDATE(),
ParamValues.x1.value('modBy[1]', 'varchar(50)')
FROM @xml.nodes('/ranks/item') AS ParamValues(x1)
END
XML 输入示例:
<ranks>
<item><itemID>25</itemID><categoryID>1</categoryID><question>some text</question><sortID>1</sortID><modBy>abc</modBy></item>
<item><itemID>12</itemID><categoryID>1</categoryID><question>some text 2</question><sortID>2</sortID><modBy>abc</modBy></item>
<item><itemID>9</itemID><categoryID>1</categoryID><question>some text 3</question><sortID>3</sortID><modBy>abc</modBy></item>
</ranks>
提前非常感谢您提供的任何帮助,蒂姆。
【问题讨论】:
标签: sql sql-server xml stored-procedures