且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用SQL Server 2008 R2删除重复ID和值ROW

更新时间:2023-02-06 23:09:36

我只能使用以下语法删除2条记录:



使用示例1;

GO

删除顶部(2)

来自Example1.dbo.RealTable

WHERE ID =''7''AND Name =''ABC''和Qty ='''6'';

GO



但是,这删除了我添加的前两个记录,而不是人们可能期望的最后两个记录。如果您可以加入另一个表并获取记录插入的某种日期时间,那么您可以命令并控制删除哪些记录。您可能还需要考虑添加某种主键或唯一索引,以便可以对行进行唯一索引。



MSDN Transact-SQL删除参考: http://msdn.microsoft.com/en-us/library/ms189835(v = sql.100).aspx [ ^ ]
I was able to delete only 2 records using the following syntax:

USE Example1;
GO
DELETE TOP (2)
FROM Example1.dbo.RealTable
WHERE ID = ''7'' AND Name = ''ABC'' AND Qty = ''6'';
GO

However, this deleted the first two records that I had added, and not the last two as one might expect. If you can join to another table and get some sort of datetime for the record insert, then you can order by and control which records are deleted. You may also want to consider adding some sort of Primary Key or Unique Index so that the rows can be uniquely indexed.

MSDN Transact-SQL Delete Reference: http://msdn.microsoft.com/en-us/library/ms189835(v=sql.100).aspx[^]