1

在最近的恢复过程中,发生了以前可能存在或不存在的问题,但是现在我知道了,我的任务是提出解决方案。我们通常将数据写入每一行的多个列名,但是我发现所有字段都将进入溢出列。下面是我们所拥有的示例。

我们应该有数据的列:

fldDate - fldName    - fldPhoneNumber - fldTransactionID - fldOverflow    
8/15/13 - John Smith - 1012023344     - 123456789        - Null

我们得到了什么:

fldDate - fldName    - fldPhoneNumber - fldTransactionID - fldOverflow   
8/15/13 - null       - null           - null             - fldName="John Smith", fldPhoneNumber="1012023344", fldTransactionID="123456789"

我需要的:

我需要找到一种方法将 fldOverflow 字段数据恢复到正确的字段中。任何帮助将不胜感激。

4

2 回答 2

0

我会将 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;
于 2013-08-15T17:05:30.677 回答
0

有许多函数可以将分隔字段分成多列,因此这是一个选项,您还可以使用COALESCE()字符串操作,例如:

SELECT  fldDate 
      , fldName = COALESCE(fldName, SUBSTRING(fldOverflow,CHARINDEX('fldName="',fldOverflow)+9,CHARINDEX(', fldPhone',fldOverflow)-CHARINDEX('fldName="',fldOverflow)-10))
      , fldPhoneNumber 
      , fldTransactionID
      , fldOverflow 
FROM Table
于 2013-08-15T16:52:55.430 回答