Delete script SQL Server 2008

后端 未结 2 671
别那么骄傲
别那么骄傲 2021-01-24 02:50

Here is a situation that I am trying to resolve. I have a table that is loaded with duplicates. It happened because similar rows were loaded from two different sources. That is

相关标签:
2条回答
  • 2021-01-24 02:57

    Maybe it will help you

    ;WITH cte AS
     (
      SELECT ROW_NUMBER() OVER (PARTITION BY UID, ADDRESS, City, State, Zip, DataSource ORDER BY UID) AS Dup
      FROM dbo.F_staRes
      )
    DELETE cte
    WHERE Dup > 1
    
    0 讨论(0)
  • 2021-01-24 03:14
    1. I think the key word INTERSECT isn't used properly. The explanation is below. You could follow the link to get detail.

    EXCEPT returns any distinct values from the left query that are not also found on the right query. INTERSECT returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand

    http://msdn.microsoft.com/zh-cn/library/ms188055.aspx

    1. To achieve your perpose, you could try the command merge.

      ;
      merge into #Clevland as target
      using  #Ohio as source
      on (target.UID = source.UID) -- you could add ADDRESS,City,State,Zip
      when not matched
         insert into target (UID) values (source.UID)
      ;
      

    Wish this will help.

    0 讨论(0)
提交回复
热议问题