-1

当我运行此 SQL 代码时,我收到以下错误消息:

Msg 116, Level 16, State 1, Line 17
Only one expression can be specified in the select list when the subquery 
is not introduced with EXISTS.

我想要的是,每当从具有相同的更新查询中返回任何内容时,它都会使用of + 1AgreementNo, ElementStartDate and DateSeqNo更新这些重复记录之一,这将删除重复项。ElementStartDate

Update [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]  
SET ElementStartDate = ElementStartDate + 1  
WHERE AgreementNo IN   
    (SELECT
        count(*) as [Count],
        [AgreementNo],
        [ElementStartDate],
        [DateSeqNo],
        getdate() as Today  
     FROM [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]   
     GROUP BY  
         [AgreementNo],
         [ElementStartDate],
         [DateSeqNo]  
     HAVING COUNT(*) = 2) 
4

3 回答 3

1

子查询返回多于一列。您不能使用 in Operator 比较多个列。

试试下面的查询:

Update [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]  
SET ElementStartDate = ElementStartDate + 1  
WHERE AgreementNo IN   
 ( SELECT [AgreementNo] 
   FROM [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]   
   GROUP BY  [AgreementNo] -- OR [AgreementNo],[ElementStartDate],[DateSeqNo]
   HAVING COUNT(*) = 2) 
于 2014-04-09T13:03:55.260 回答
1

在 的分区中分配行号AgreementNo, ElementStartDate, DateSeqNo并更新行号大于 1 的那些行:

WITH ranked AS (
  SELECT
    ElementStartDate,
    rn = ROW_NUMBER() OVER (PARTITION BY AgreementNo, ElementStartDate, DateSeqNo
                                ORDER BY (SELECT 1))  -- actual order probably
                                                      -- doesn't matter here
  FROM WASP_Mart_EmbassyActuarial.dbo.tblARM_OmegaSource
)
UPDATE ranked
SET ElementStartDate = ElementStartDate + rn - 1
WHERE rn > 1
;

这种方法可以处理一个组中有两个以上重复的案例,当然,如果有很多重复,它可能会开始产生新的。

于 2014-04-09T18:45:46.867 回答
0

也许试试这个:

Update [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]  
SET ElementStartDate = ElementStartDate + 1  
WHERE AgreementNo IN   
 (SELECT 
      AgreementNo 
  FROM [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]   
  GROUP BY 
      [AgreementNo],
      [ElementStartDate],
      [DateSeqNo]
  HAVING COUNT(*) = 2)
于 2014-04-09T13:05:44.403 回答