CREATE TABLE #HR_XML (ID INT IDENTITY, Salaries XML)
GO
INSERT #HR_XML VALUES(
'<Salaries>
<Marketing>
<Employee>
<Salary>42000</Salary>
<Incentive>12000</Incentive>
<LeaveEncashment>12000</LeaveEncashment>
</Employee>
</Marketing>
</Salaries>
')
Select * from #HR_XML
Declare @Node Varchar(100) = ''
SET @Node = 'LeaveEncashment'
UPDATE #HR_XML
SET Salaries.modify('replace value of
(/Salaries/Marketing/Employee/[@Node]/text())[1] with ("60000")')
GO
Select * from #HR_XML
Drop Table #HR_XML
从上面的sql查询,如何动态更新/传递@node
答案 0 :(得分:-1)
您应该使用Dynamic sql
CREATE TABLE #HR_XML (ID INT IDENTITY, Salaries XML)
GO
INSERT #HR_XML VALUES(
'<Salaries>
<Marketing>
<Employee>
<Salary>42000</Salary>
<Incentive>12000</Incentive>
<LeaveEncashment>12000</LeaveEncashment>
</Employee>
</Marketing>
</Salaries>
')
Select * from #HR_XML
Declare @Node Varchar(100) = ''
SET @Node = 'LeaveEncashment'
declare @sql varchar(max)
Set @sql = ' UPDATE #HR_XML
SET Salaries.modify(''replace value of
(/Salaries/Marketing/Employee/'+@Node+'/text())[1] with ("60000")'')'
exec (@sql)
GO
Select * from #HR_XML
Drop Table #HR_XML