关于SQL Server中的删除命令

时间:2016-02-03 10:34:24

标签: sql sql-server sql-delete

如何从表中删除重复的行?

1 个答案:

答案 0 :(得分:2)

我喜欢CTE和ROW_NUMBER,因为它允许轻松更改它以查看哪些行被删除(或更新),因此只需将DELETE FROM CTE ...更改为SELECT * FROM CTE:

;WITH CTE AS (
    SELECT [col1],
           [col2],
           [col3],
           [col4],
           [col5],
           [col6],
           [col7],
           RN = ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1)
    FROM dbo.table
)
DELETE FROM CTE
WHERE RN > 1

试试这个