我有一个存储过程,其中包含一些输入参数,包括XML参数。我有一种情况,我需要使用直接输入字段之一,还有另一列XML输入用于循环并检查相应的不同表。示例如下。
Procedure A
@ID varchar
@xml XML
我有两个表TableA,TableB。
TableA contains ID.
TableB contains @xml's fields named X,Y.
现在我需要在我的SP中编写我的逻辑
如果TableA包含ID,则更新它,或者根据输入值创建新ID。
检查tableB中@XML中的值X,如果存在,则更新它,否则写一个新值。
写一个小的temptable用于存储XML(即X,Y)值的值,并使用它的值进行检查。
请帮我这个结构。我会发展。
答案 0 :(得分:2)
草图:
CREATE PROCEDURE [dbo].[procedure_name]
@ID VARCHAR(100)
,@xml XML
AS
BEGIN
SET NOCOUNT ON;
/* Write a small temptable for storing the values of XML (i.e X,Y)
values and use it's values for checking */
CREATE TABLE #temp(col_1 INT
,col_2 NVARCHAR(100)
(...)
);
INSERT INTO #temp(col_1, col_2, ...)
SELECT
[col_1] = col.value('col_name[1]','INT'),
[col_2] = col.value('col_name[2]', 'NVARCHAR(100)')
FROM @xml.value('/node') AS T(col);
/* if TableA contains ID then update it or else create a new ID
as per input value */
IF EXISTS (SELECT 1 FROM TableA WHERE ID = @ID)
UPDATE tabA
SET col_1 = ....
FROM TableA A
WHERE ID = @ID
ELSE
INSERT INTO TableA(col_1, col_2, ...)
SELECT col_1, ...
FROM #temp
/* Check tableB for value X in @XML, if it exists, then update it,
else write a new value. */
IF EXISTS (SELECT 1
FROM TableB tab_b
JOIN #temp t
ON tab_b.id = t.id)
BEGIN
-- Update here
END
ELSE
BEGIN
-- Insert here
END
END
答案 1 :(得分:0)
我在解析XML时完成了几乎相同的任务。它非常依赖于XML结构。我可以分享我的代码,也许它适合你的情况。
XML:
<ROOT>
<ResultOfTest Sort="2" description="Test result" is_bp_enabled="True" max_error="0.48089456118941">
<PROPERTY Name="InputId" Description="Sample number" Value="0" Sort="1" />
<PROPERTY Name="InputDescr" Description="Input vector description" Value="No descr" Sort="2" />
<PROPERTY Name="OutputId" Description="Smple number" Value="0" Sort="3" />
<PROPERTY Name="OutputDescr" Description="Output vector description" Value="No descr" Sort="4" />
<PROPERTY Name="ErrorValue" Description="Quantitative measure of error" Value="0.291986530595333" Sort="5" />
<PROPERTY Name="InputId" Description="Sample number" Value="1" Sort="1" />
<PROPERTY Name="InputDescr" Description="Input vector description" Value="No descr" Sort="2" />
<PROPERTY Name="OutputId" Description="Smple number" Value="1" Sort="3" />
<PROPERTY Name="OutputDescr" Description="Output vector description" Value="No descr" Sort="4" />
<PROPERTY Name="ErrorValue" Description="Quantitative measure of error" Value="0.291986527462545" Sort="5" />
<PROPERTY Name="InputId" Description="Sample number" Value="2" Sort="1" />
<PROPERTY Name="InputDescr" Description="Input vector description" Value="No descr" Sort="2" />
<PROPERTY Name="OutputId" Description="Smple number" Value="2" Sort="3" />
<PROPERTY Name="OutputDescr" Description="Output vector description" Value="No descr" Sort="4" />
<PROPERTY Name="ErrorValue" Description="Quantitative measure of error" Value="0.292008233355948" Sort="5" />
<PROPERTY Name="InputId" Description="Sample number" Value="3" Sort="1" />
<PROPERTY Name="InputDescr" Description="Input vector description" Value="No descr" Sort="2" />
<PROPERTY Name="OutputId" Description="Smple number" Value="3" Sort="3" />
<PROPERTY Name="OutputDescr" Description="Output vector description" Value="No descr" Sort="4" />
<PROPERTY Name="ErrorValue" Description="Quantitative measure of error" Value="0.291986537162087" Sort="5" />
</ResultOfTest>
</ROOT>
我选择解析它:
SELECT
item.value('@Name', 'varchar(100)') AS [Name],
item.value('@Description', 'varchar(200)') AS [Description],
item.value('@Value', 'varchar(200)') AS [Value],
item.value('@Sort', 'varchar(10)') AS [Sort]
FROM @i_xml.nodes('/ROOT/ResultOfTest/PROPERTY') AS t(item)
您可以将其与表格连接,然后将结果插入目标表格。