我会将 fldOverflow 值转换为 xml,然后我会使用 UPDATE :
DECLARE @MyTable TABLE(
fldName NVARCHAR(50),
fldPhoneNumber VARCHAR(20),
fldTransactionID INT,
fldOverflow NVARCHAR(4000)
);
INSERT INTO @MyTable (fldOverflow)
VALUES (N'fldName="John Smith", fldPhoneNumber="1012023344", fldTransactionID="123456789"');
PRINT 'Before:'
SELECT *
FROM @MyTable;
-- I would use, also, transactions thus
-- BEGIN TRANSACTION
UPDATE x
SET fldName =z.fldName,
fldPhoneNumber =z.fldPhoneNumber,
fldTransactionID=z.fldTransactionID,
fldOverflow =NULL
-- OUTPUT deleted.fldName AS OldfldName,inserted.fldName AS NewfldName, ...
FROM @MyTable x
CROSS APPLY
(
SELECT y.XmlFragment.value('(/row/@fldName)[1]','NVARCHAR(50)') AS fldName,
y.XmlFragment.value('(/row/@fldPhoneNumber)[1]','VARCHAR(20)') AS fldPhoneNumber,
y.XmlFragment.value('(/row/@fldTransactionID)[1]','INT') AS fldTransactionID
FROM
(
SELECT CONVERT(XML,'<row '+REPLACE(x.fldOverflow,',', ' ')+' />') AS XmlFragment
) y
) z
WHERE x.fldOverflow IS NOT NULL;
-- ROLLBACK TRANSACTION
-- -- COMMIT TRANSACTION
PRINT 'After:'
SELECT *
FROM @MyTable;
结果:
Before:
fldName fldPhoneNumber fldTransactionID fldOverflow
----------- --------------- ---------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NULL NULL NULL fldName="John Smith", fldPhoneNumber="1012023344", fldTransactionID="123456789"
After:
fldName fldPhoneNumber fldTransactionID fldOverflow
----------- --------------- ---------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
John Smith 1012023344 123456789 NULL
编辑1:
SELECT x.fldName AS OldfldName,z.fldName AS NewfldName, ...
FROM @MyTable x
CROSS APPLY
(
SELECT y.XmlFragment.value('(/row/@fldName)[1]','NVARCHAR(50)') AS fldName,
y.XmlFragment.value('(/row/@fldPhoneNumber)[1]','VARCHAR(20)') AS fldPhoneNumber,
y.XmlFragment.value('(/row/@fldTransactionID)[1]','INT') AS fldTransactionID
FROM
(
SELECT CONVERT(XML,'<row '+REPLACE(x.fldOverflow,',', ' ')+' />') AS XmlFragment
) y
) z
WHERE x.fldOverflow IS NOT NULL;