Assuming Employee(Empid,name,salary) exists with duplicate records with Empid,name,salary in an SQL Server 2017 table.
IF OBJECT_ID('TempDB..#Employee') IS NOT NULL
DROP TABLE #Employee
CREATE TABLE #Employee
(
Empid INT,
name VARCHAR(250),
salary FLOAT
)
INSERT INTO #Employee
VALUES (1008, 'Biju Joseph', 8500),
(1008, 'Biju Joseph', 8500),
(1008, 'Haris', 9000),
(1009, 'John', 9500),
(1009, 'John', 9500),
(1010, 'SMITH', 10500)
This nested query is working fine to SELECT (with SQL Server 2017):
SELECT *
FROM
(SELECT
name,
ROW_NUMBER() OVER (PARTITION BY name, salary ORDER BY name) cnt
FROM
employee) mytable
WHERE
cnt > 1
but, when try to DELETE, it is showing an error message:
(DELETE FROM <tablename> WHERE <expression>)
DELETE FROM
(SELECT
name,
ROW_NUMBER() OVER(PARTITION BY name, salary ORDER BY name) cnt
FROM
employee) mytable
WHERE cnt > 1
Errors:
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near '('.Msg 102, Level 15, State 1, Line 20
Incorrect syntax near 'mytable'
Please note : when using a CTE, or other subqueries, I am able to DELETE, but what I am missing in this particular subquery, when trying to DELETE?
delete [mytable] from (...