2011-07-07 141 views
0

對於更新海量記錄,我使用了xml查詢。從前端(C#.net)填充xml並將其作爲參數傳遞給存儲過程(如@Master_rows_xml_Update)。SQL Server 2005中的XML查詢錯誤

我面對的情況。當我在存儲過程中

declare @i INTEGER,@Master_rows_xml_Update XML; 

SET @Master_rows_xml_Update= '<root> 
           <row Id="1" FinYearId="5" EmployeeId="55" EnteredOn="7/1/2011 12:00:00 AM" EnteredBy="1" HouseRentPaid="False" HouseRentAmount="3500.00" officeId="9"/> 
           <row Id="2" FinYearId="5" EmployeeId="55" EnteredOn="7/1/2011 12:00:00 AM" EnteredBy="1" HouseRentPaid="False" HouseRentAmount="3500.00" officeId="9"/> 
           </root>'; 

exec sp_xml_preparedocument @i output, @Master_rows_xml_Update; 

update EmployeeFinYearInvestment 
    set EmployeeFinYearInvestment.FinYearId = ox.FinYearId, 
    EmployeeFinYearInvestment.EmployeeId = ox.EmployeeId, 
    EmployeeFinYearInvestment.EnteredOn = ox.EnteredOn, 
    EmployeeFinYearInvestment.EnteredBy = ox.EnteredBy, 
    EmployeeFinYearInvestment.HouseRentPaid = ox.HouseRentPaid, 
    EmployeeFinYearInvestment.HouseRentAmount = ox.HouseRentAmount, 
    EmployeeFinYearInvestment.officeId=ox.officeId 

from OpenXml(@i, 'root/row') 
    with (Id INT,FinYearId INT, EmployeeId INT,EnteredOn DATETIME,EnteredBy INT , HouseRentPaid BIT,HouseRentAmount DECIMAL ,officeId INT) ox 
    where EmployeeFinYearInvestment.Id = ox.Id; 

exec sp_xml_removedocument @i; 

..它不能更新ACTION見下表去爲這個命令

程序也不會返回任何錯誤...

任何想法...

謝謝.. Anirban

+0

您是否試圖調試此代碼?它如何選擇XML?是否存在來自'EmployeeFinYearInvestment.Id = ox.Id;'子句的項目? – VMAtm

回答

1

我會嘗試擺脫OpenXml和這些功能 - 使用SQL Server 2005中新的XQuery/XPath的東西!

嘗試這一點 - 這應該給你從你的XML值:

DECLARE @XmlUpdate XML 
SET @XmlUpdate = 
    '<root> 
    <row Id="1" FinYearId="5" EmployeeId="55" EnteredOn="7/1/2011 12:00:00 AM" EnteredBy="1" HouseRentPaid="False" HouseRentAmount="3500.00" officeId="9"/> 
    <row Id="2" FinYearId="5" EmployeeId="55" EnteredOn="7/1/2011 12:00:00 AM" EnteredBy="1" HouseRentPaid="False" HouseRentAmount="3500.00" officeId="9"/> 
    </root>'; 

SELECT 
    UpdRow.value('(@Id)[1]', 'int') AS 'ID', 
    UpdRow.value('(@FinYearId)[1]', 'int') AS 'FinYearID', 
    UpdRow.value('(@EmployeeId)[1]', 'int') AS 'EmployeeID', 
    UpdRow.value('(@EnteredOn)[1]', 'datetime') AS 'EnteredOn', 
    UpdRow.value('(@EnteredBy)[1]', 'int') AS 'EnteredBy', 
    UpdRow.value('(@HouseRentPaid)[1]', 'bit') AS 'RentPaid', 
    UpdRow.value('(@HouseRentAmount)[1]', 'decimal(20,4)') AS 'RentAmount', 
    UpdRow.value('(@OfficeId)[1]', 'int') AS 'OfficeID' 
FROM 
    @XmlUpdate.nodes('/root/row') AS R(UpdRow) 

和當然,如果你可以選擇它,你也可以用它來更新!

UPDATE 
    dbo.EmployeeFinYearInvestment 
SET 
    EmployeeFinYearInvestment.FinYearId = UpdRow.value('(@FinYearId)[1]', 'int'), 
    EmployeeFinYearInvestment.EmployeeId = UpdRow.value('(@EmployeeId)[1]', 'int'), 
    ..... (and so on). ...... 
    EmployeeFinYearInvestment.EnteredOn = ox.EnteredOn, 
FROM 
    @XmlUpdate.nodes('/root/row') AS R(UpdRow) 
WHERE 
    EmployeeFinYearInvestment.Id = UpdRow.value('(@Id)[1]', 'int');