1

我的数据库中有一组如下所示的行:

Mr Joe Bloggs
Joe Bloggs
Joe Jim Bloggs
Joe Jim David Bloggs
Mr Joe Jim Bloggs
Mr Joe Jim David Bloggs

需要输出:

Title | First Name | Middle Name | Surname
------------------------------------------
Mr | Joe | x | Bloggs
x | Joe | x | Bloggs
x | Joe | Jim | Bloggs
x | Joe | Jim David | Bloggs
Mr | Joe | Jim | Bloggs
Mr | Joe | Jim David | Bloggs

这将在 SQL 存储过程中运行。我需要的是在循环通过 CURSOR 和 4 个变量以更新数据库中的 4 列时生成的输出。但正如您所见,数据在 1 列中并且各不相同。

我发现很难理解这样做的最佳方式,并希望得到您的帮助。需要帮助请叫我。

与我的类似问题,但我觉得这些并没有给我我需要的完整答案,或者我认为它没有回答我的问题:

编辑:

    --Temporary Table to Merge the full name together in order to split by comma. Each split will have a unique record but with the same address information for each name. 
    CREATE TABLE #TempOutput
    (
        ClientReference varchar(MAX),
        Prefix varchar(MAX),
        Reference int,
        MergedFullName varchar(MAX),
        FormatName varchar(MAX),
        Title varchar(MAX),
        FirstName varchar(MAX),
        MiddleName varchar(MAX),
        Surname varchar(MAX)
    )

    DECLARE @Ref varchar(MAX)
    DECLARE @Prefix varchar(MAX)
    DECLARE @Reference int
    DECLARE @MergedName varchar(MAX)
    DECLARE @FormatName varchar(MAX)
    DECLARE @Title varchar(MAX)
    DECLARE @FirstName varchar(MAX)
    DECLARE @MiddleName varchar(MAX)
    DECLARE @Surname varchar(MAX)

    DECLARE ExtraNameSplitCursor CURSOR FOR
        SELECT 
            [Client Reference], 
            Prefix,
            Reference,
            CASE 
                WHEN LEFT(Surname,3) = 'Mr,' THEN (Title + ' ' + [First Name] + ' ' + [Middle Name] + ' ' + Replace(Surname,'Mr,',''))
                WHEN LEFT(Surname,4) = 'Mrs,' THEN (Title + ' ' + [First Name] + ' ' + [Middle Name] + ' ' + Replace(Surname,'Mrs,',''))
                ELSE (Title + ' ' + [First Name] + ' ' + [Middle Name] + ' ' + Surname) 
            END AS [Merged Full Name]
        FROM #StartResults

    OPEN ExtraNameSplitCursor

    FETCH NEXT FROM ExtraNameSplitCursor INTO @Ref, @Prefix, @Reference, @MergedName
    WHILE @@FETCH_STATUS = 0
        BEGIN
            INSERT INTO #TempOutput (ClientReference, Prefix, Reference, MergedFullName) 
            SELECT @Ref, @Prefix, @Reference, GUID, FROM dbo.fn_SPLIT_LIST_STRING (@MergedName)

            --SELECT CASE 
            --  WHEN @FormatName.GUID = 'Mr ' THEN @Title = 'Mr'
            --  WHEN LEFT(@MergedName,4) = 'Mrs ' THEN @Title = 'Mrs' 
            --  WHEN LEFT(@MergedName,5) = 'Miss ' THEN @Title = 'Miss'
            --  WHEN LEFT(@MergedName,3) = 'Ms ' THEN @Title = 'Ms'
            --  WHEN LEFT(@MergedName,3) = 'Dr ' THEN @Title = 'Dr'
            --  WHEN LEFT(@MergedName,4) = 'Rev ' THEN @Title = 'Rev'
            --  WHEN LEFT(@MergedName,5) = 'Lord ' THEN @Title = 'Lord'
            --  ELSE @Title = '' 
            --END AS Title,
            --CASE 
            --  WHEN MID(@MergedName,2,1) = ' ' THEN @FirstName = '' ELSE @FirstName = CHARINDEX(' ', MergedFullName + ' ') FROM #TempOutput AS FirstName

            FETCH NEXT FROM ExtraNameSplitCursor INTO @Ref, @Prefix, @Reference, @MergedName, @FlatNo, @HouseName, @HouseNo, @Street, @District, @PostTown, @County, @PostCode
        END

    CLOSE ExtraNameSplitCursor
    DEALLOCATE ExtraNameSplitCursor

    --Select statement for temporary table
    SELECT 
        ISNULL(ClientReference,'') AS ClientReference,
        ISNULL(Prefix,'') AS Prefix,
        ISNULL(Reference,'') AS Reference,
        ISNULL(MergedFullName,'') AS MergedFullName, 
        ISNULL(Title,'') AS Title,
        ISNULL(FirstName,'') AS FirstName,
        ISNULL(MiddleName,'') AS MiddleName,
        ISNULL(Surname,'') AS Surname

    FROM #TempOutput Result
    ORDER BY ClientReference
    for xml auto

    --Drop all temporary tables
    DROP TABLE #TempOutput
    DROP TABLE #StartResults

END
4

1 回答 1

0

我已经解决了我的问题,并感谢您提供的线索和帮助。

我基本上添加了第二个光标,它执行以下操作:

  • 抓住合并的全名字段并放入一个临时变量。
  • 通过运行 case 语句来检查标题以检查 Mr、Mrs 等。否则在 Title 中输入空白字符串。
  • 更新标题修剪合并全名的标题
  • 通过 RIGHT(@TempName, CHARINDEX(' ', REVERSE(@TempName)) - 1) 检查姓氏,并假设这是姓氏。
  • 更新姓氏修剪并替换合并的全名
  • 通过 LEFT(@TempName, CHARINDEX(' ',
    @TempName + ' ') -1)检查左边的名字
  • 更新名字修剪并替换合并的全名
  • 其他任何东西都放在中间名中。

有点啰嗦,但在我的数据库中 99.9% 的情况下,它的格式都很完美。

如果您想要上面的代码,请告诉我。

谢谢你的时间。

于 2012-12-03T14:57:09.163 回答