您應該刪除變量@xmldoc和INSERT這樣的查詢使用
CAST(@DocParam as XML)
:
; With c as(SELECT CAST(@DocParam as XML) DocParam)
SELECT TBL.SParam.value('local-name(.)[1]', 'varchar(50)') as Name,
TBL.SParam.value('(.)[1]', 'varchar(50)') as Value1,
TBL2.SParam2.value('(.)[1]', 'varchar(50)') as Value2,
TBL3.SParam3.value('(.)[1]', 'varchar(50)') as Value3,
TBL4.SParam4.value('(.)[1]', 'varchar(50)') as Value4,
TBL5.SParam5.value('(.)[1]', 'varchar(50)') as Value5
FROM C
CROSS APPLY
DocParam.nodes('/NewDataSet/Table1[1]/*') AS TBL(SParam)
left JOIN DocParam.nodes('/NewDataSet/Table1[2]/*') AS TBL2(SParam2)
ON TBL.SParam.value('local-name(.)[1]', 'varchar(50)')
= TBL2.SParam2.value('local-name(.)[1]', 'varchar(50)')
left JOIN DocParam.nodes('/NewDataSet/Table1[3]/*') AS TBL3(SParam3)
ON TBL.SParam.value('local-name(.)[1]', 'varchar(50)')
= TBL2.SParam2.value('local-name(.)[1]', 'varchar(50)')
left JOIN DocParam.nodes('/NewDataSet/Table1[4]/*') AS TBL4(SParam4)
ON TBL.SParam.value('local-name(.)[1]', 'varchar(50)')
= TBL2.SParam2.value('local-name(.)[1]', 'varchar(50)')
left JOIN DocParam.nodes('/NewDataSet/Table1[5]/*') AS TBL5(SParam5)
ON TBL.SParam.value('local-name(.)[1]', 'varchar(50)')
= TBL2.SParam2.value('local-name(.)[1]', 'varchar(50)')
第二個選項是有2個數據集,第一臺@xmldoc參數的值,第二查詢中使用參數
第三個選項是使用存儲過程和所有內部邏輯