CREATE TABLE test_delete(
name varchar(10),
value INT
);
go
-- 张三100 与 王五80 是有重复的
INSERT INTO test_delete
SELECT '张三', 100
UNION ALL SELECT '张三', 100
UNION ALL SELECT '李四', 80
UNION ALL SELECT '王五', 80
UNION ALL SELECT '王五', 80
UNION ALL SELECT '赵六', 90
UNION ALL SELECT '赵六', 70
go
-- 测试检索数据
SELECT
ROW_NUMBER() OVER (PARTITION BY name, value ORDER BY (SELECT 1) ) AS no,
name,
value
FROM
test_delete
no name value
-------------------- ---------- -----------
1 李四 80
1 王五 80
2 王五 80
1 张三 100
2 张三 100
1 赵六 70
1 赵六 90
--创建视图
CREATE VIEW tmp_view AS
SELECT
ROW_NUMBER() OVER (PARTITION BY name, value ORDER BY (SELECT 1) ) AS no,
name,
value
FROM
test_delete
--删除数据
1> DELETE FROM tmp_view WHERE no != 1
2> go
(2 行受影响)
-- 核对结果
1> www.jb51.net
2> select * from test_delete;
3> go
name value
---------- -----------
张三 100
李四 80
王五 80
赵六 90
赵六 70
(5 行受影响)
作者 tearsmo