我有其中一个要更新。
关闭,但没有雪茄。但这是一天的结束。
IF OBJECT_ID('tempdb..#XmlHolderTable') IS NOT NULL
begin
drop table #XmlHolderTable
end
IF OBJECT_ID('tempdb..#ScalarHolderTable') IS NOT NULL
begin
drop table #ScalarHolderTable
end
CREATE TABLE #ScalarHolderTable
(
ScalarKey int not null ,
Name varchar(16) ,
Phone varchar(16) ,
UUID uniqueidentifier
)
CREATE TABLE #XmlHolderTable
(
XmlSurrogateIdentityKey int not null identity (1001, 1),
TheXml xml
)
INSERT INTO #ScalarHolderTable
( ScalarKey , Name , Phone , UUID )
select 1 , 'HK' , 999 , NEWID()
union all select 2 , 'RK' , 888 , NEWID()
union all select 3 , 'SK' , 777 , NEWID()
union all select 4 , 'PK' , 66 , NEWID()
-- Declare XML variable
DECLARE @data XML;
-- Element-centered XML
SET @data = N'
<Root>
<Data1>
<ID>1</ID>
<Name>HK</Name>
</Data1>
<Data1>
<ID>2</ID>
<Name>RK</Name>
</Data1>
</Root>
';
INSERT INTO #XmlHolderTable ( TheXml) values ( @data )
select TheXml.value('(//Data1/ID)[1]','int') , * from #XmlHolderTable
SELECT Data.Col.value('(.)[1]','Int') AS Id
FROM #XmlHolderTable xmlHolder
CROSS APPLY
TheXml.nodes('//Data1/ID') AS Data(Col)
/*
SELECT Data.Col.value('(Id)[1]','Int') AS Id
FROM @Data.nodes('/Root/Data') AS Data(Col)
*/
declare @counter int
select @counter = 0
/*
WHILE (
exists ( select top 1 null
From
#XmlHolderTable xmlHolder
CROSS APPLY
TheXml.nodes('//Data1/ID') AS Data(Col) , #ScalarHolderTable scalarHolder
Where
ISNUMERIC ( Data.Col.value('(.)[1]','varchar(40)') ) > 0
)
)
BEGIN
select @counter= @counter + 1
print '/@counter/'
print @counter
print ''
*/
UPDATE
#XmlHolderTable
SET
TheXml.modify('replace value of (//Data1/ID/text())[1] with sql:column("scalarHolder.UUID")')
--select Data.Col.value('(.)[1]','Int') as MyValue , scalarHolder.ScalarKey
From
#XmlHolderTable xmlHolder CROSS APPLY TheXml.nodes('//Data1/ID') AS Data(Col)
, #ScalarHolderTable scalarHolder
Where
Data.Col.value('(.)[1]','Int') = scalarHolder.ScalarKey
/*
END
*/
select * from #ScalarHolderTable
select TheXml from #XmlHolderTable
IF OBJECT_ID('tempdb..#XmlHolderTable') IS NOT NULL
begin
drop table #XmlHolderTable
end
IF OBJECT_ID('tempdb..#ScalarHolderTable') IS NOT NULL
begin
drop table #ScalarHolderTable
end