If you need to delete the rows from the table and you can't create a new table, you need to know the columns in order to create a condition defining what is duplicate.
Having that said, it is enough that you pass the name of the table to the procedure. You can get rest of the information from
sys.columns[
^] system view. This enables you to create a SQL statement dynamically based on the table name and then execute the dynamic statement.
So the basic idea for the procedure would be
- get the table name as a parameter
- fetch the names of the columns in that table
- build the DELETE statement into a NVARCHAR variable
- execute the statement using
sp_executesql[
^]
For the DELETE statement, there are a lot of ways to differentiate the duplicate records in a table. One way is to use the physical location as described in
How to remove duplicate rows in SQL Server 2008 when no key is present[
^]