0

我有 2 张桌子

表 A

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City  State 
NULL      Micheal    Calvin      Dodson    12     23     1234  123  XYZ   ABC    
NULL      John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG    
NULL      John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG 
NULL      William    Stephen     NULL      45     NULL   NULL  NULL HJD   ABC
NULL      Victor     NULL        Anthony   NULL   NULL   NULL  NULL NULL  NULL

表 B

NameID   FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City State Zip Email Gender...

我需要获取表 A 的 (FirstName,MiddleName,LastName) 的不同记录,并将相同的详细信息以及与表 A 匹配的其他字段插入表 B。我的表 B 将 NameID 作为标识列。因此,在将唯一记录插入表 B 后,我需要获取该 NameID 并将其插入回表 A 中,如下所示:

表A

表 A

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City  State 
1         Micheal    Calvin      Dodson    12     23     1234  123  XYZ   ABC    
2         John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG    
2         John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG 
3         William    Stephen     NULL      45     NULL   NULL  NULL HJD   ABC
4         Victor     NULL        Anthony   NULL   NULL   NULL  NULL NULL  NULL

表 B

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City State Zip  Email Gender...
1       Micheal    Calvin      Dodson    12     23     1234  123   XYZ   ABC   NULL NULL  NULL
2       John       NULL        Keith     NULL   NULL   2344  NULL  SQE   FDG   NULL NULL  NULL
3       William    Stephen     NULL      45     NULL   NULL  NULL  HJD   ABC   NULL NULL  NULL
4       Victor     NULL        Anthony   NULL   NULL   NULL  NULL  NULL  NULL  NULL NULL  NULL

你能帮我解决这个问题吗?我无法正确获取此查询。SQL Server 2008 中的代码

在此先感谢,
苏尼莎

4

2 回答 2

0
DECLARE @results TABLE
(
  NameID INT, 
  FirstName VARCHAR(32), -- guessing on data types for these columns 
  MiddleName VARCHAR(32), 
  LastName VARCHAR(32)
);

;WITH x AS 
(
 SELECT FirstName, MiddleName, LastName, 
     rn = ROW_NUMBER() OVER (PARTITION BY FirstName, MiddleName, LastName
      ORDER BY (SELECT NULL)
   ) --, ... other columns ...
   FROM dbo.TableA
)
INSERT dbo.TableB 
(
  FirstName, MiddleName, LastName --, ... other columns ...
)
OUTPUT 
  inserted.NameID, inserted.FirstName, 
  inserted.MiddleName, inserted.LastName
INTO @results
SELECT FirstName, MiddleName, LastName --, ... other columns ...
FROM x WHERE rn = 1;

UPDATE a SET NameID = r.NameID
  FROM dbo.TableA AS a
  INNER JOIN @results AS r
  ON COALESCE(a.FirstName,'') = COALESCE(r.FirstName,'')
  AND COALESCE(a.MiddleName,'') = COALESCE(r.MiddleName,'')
  AND COALESCE(a.LastName,'') = COALESCE(r.LastName,'');
于 2012-06-26T16:50:09.497 回答
0

我认为最简单的方法是使用两个查询。第一个问题是处理 TableA 中的重复项。以下查询为每个名称组合选择任意行:

插入 TableB() select () from (select a.*, row_number() over (partition by FirstName, MiddleName, LastName order by FirstName) as seqnum from TableA a ) a where seqnum = 1

然后,更新原始表:

update TableA
    set NameId = (select max(NameId) from TableB
                  where TableB.FirstName = TableA.FirstName and
                        TableB.MiddleName = TableA.MiddleName and
                        TableB.LastName = TableA.LastName
                 )
    where NameId is null

如果您的字段包含 NULL 值(而不是空白),则可以将 coalesce() 用于连接条件:

update TableA
    set NameId = (select max(NameId) from TableB
                  where coalesce(TableB.FirstName, '<null>') = coalesce(TableA.FirstName, '<null>') and
                        coalesce(TableB.MiddleName, '<null>') = coalesce(TableA.MiddleName, '<null>') and
                        coalesce(TableB.LastName , '<null>')= coalesce(TableA.LastName, '<null>')
                 )
    where NameId is null
于 2012-06-26T15:49:16.537 回答