user1605859 user1605859 - 1 year ago 47 Question

How to delete top n rows of a SQL Server table in

I want to delete (for example) top 5 rows in my SQL Server database. I used this code shown below, but it does not work. How may I get this?

Dim con As New SqlConnection(connectionString)

Using cmd = New SqlCommand()
cmd.CommandText = "DELETE From PubTb TOP 5"
cmd.Connection = con
Dim numberDeleted As Integer = cmd.ExecuteNonQuery()
End Using


See this link

Change it as per your table and Database name. Also order it if you want to

  cmd.CommandText = ";WITH CTE AS ( SELECT TOP 5 * FROM PubTb )  DELETE FROM CTE"