我一直在寻找代码,但是我无法在SQL Server 2017中使用这些代码。
我需要创建一个存储过程,以避免表中的数据重复,并删除所有重复的数据。
我创建了以下代码:
CREATE PROCEDURE deldupl_LSBU_Staff AS
SELECT Phone_number, COUNT(*) as CNT
FROM LSBU_Staff
GROUP BY Phone_number
DELETE FROM LSBU_Staff
WHERE Phone_number > 1;
但是,当我执行代码时,它会删除表中的所有记录,而我不希望这样做。我只想删除所有重复的数据。
我还创建了另一个代码,以从表LSBU_Staff
中删除重复的数据:
SELECT ROW_NUMBER() OVER(PARTITION BY Phone_number ORDER BY Phone_number)
AS del_dupl_record
FROM LSBU_Staff
WHERE Phone_number > 1
DELETE FROM LSBU_Staff
WHERE Phone_number > 1;
它仍然会删除所有数据。
LSBU_Staff
列是:Staff_id
,LastName
,FirstName
,Speciality_type
和Phone_number
。我选择Phone_number
作为其标识。
答案 0 :(得分:0)
尝试一下。不是超级优雅,可以清理,但应该可以解决。这将保持组的第一。如果您希望保留对“ l2.Staff_id> l1.Staff_id”的最后更改
-
DROP TABLE IF EXISTS LSBU_Staff
CREATE TABLE LSBU_Staff
( Staff_id INT IDENTITY(1,1)
, LastName VARCHAR(32)
, FirstName VARCHAR(32)
, Speciality_type VARCHAR(32)
, Phone_number VARCHAR(32)
)
INSERT INTO LSBU_Staff (LastName, FirstName, Speciality_type, Phone_number)
VALUES
('Stilskin', 'Rumple', 'dancer' , '305-305-3050')
, ('Lamb', 'Mary', 'shepherd' , '305-123-4567')
, ('Lamb', 'Aurthur', 'shepherd' , '305-123-4567')
, ('Fenokee', 'Okee', 'swimmer' , '305-305-3051')
SELECT * FROM LSBU_Staff
DELETE LSBU_Staff
WHERE Staff_id IN
(
SELECT Staff_id
FROM LSBU_Staff l1
WHERE EXISTS (SELECT 1 FROM LSBU_Staff l2 WHERE l2.Phone_number = l1.Phone_number
AND l2.Staff_id < l1.Staff_id)
)
SELECT * FROM LSBU_Staff
DROP TABLE IF EXISTS LSBU_Staff