我已经创造了这个spx,但我需要它是动态的,在某种意义上是动态的,它应该接受
表名 列 按列排序
所以我可以让这个spx在所有表格中全局工作,只是说在这里我不发送我将要删除的表格的pk
ALTER PROCEDURE [dbo].[spx_DeleteDuplicates]
AS
BEGIN
WITH DupsNumbered
AS (
SELECT [columns List],
ROW_NUMBER() OVER (PARTITION BY [columns List] ORDER BY [orderList]) AS rn
FROM [table]
)
DELETE DupsNumbered
WHERE rn > 1;
END
您在[]
中看到的所有项目都需要是动态的,我怎样才能让它变得动态
答案 0 :(得分:0)
不是动态SQL的巨大支持者,但如果你必须
ALTER PROCEDURE [dbo].[spx_DeleteDuplicates] (@TableName varchar(100),@ColumnList varchar(max),@OrderList varchar(max))
AS
BEGIN
Declare @SQL varchar(max)='
WITH DupsNumbered
AS (
SELECT [ColumnsList],
ROW_NUMBER() OVER (PARTITION BY [ColumnsList] ORDER BY [OrderList]) AS rn
FROM [TableName]
)
DELETE DupsNumbered
WHERE rn > 1;
'
Set @SQL=Replace(@SQL,'[TableName]' ,@TableName)
Set @SQL=Replace(@SQL,'[ColumnsList]',@ColumnList)
Set @SQL=Replace(@SQL,'[OrderList]' ,@OrderList)
Exec(@SQL)
END
答案 1 :(得分:0)
如果您的行完全重复,我会保持简单:
Select distinct *
Into #temptable
From [table] ;
Truncate table [table];
Select *
Into [table]
From #temptable