Question

I use SQL Server 2014 to create a database and a table and inserted 10 rows of data.

The problem that when I select to 1000 rows, rows was repeated 4 times so they are now 40 rows as you can see in the images

first 10 rows

Last 10 rows

Now I want to ask: why did this happen?

And how to solve that?

And also I want to ask how to find the query that I entered the data in .. I tried script table as > CREATE TO > create new query but it only gives me the table and values

Was it helpful?

Solution

How did you try and delete the rows? Did you write a DELETE statement and specify an id? Given you have 4 sets of rows that you say are unique (I'll take your word for it) and you have an ID column which repeats, you could adapt the following statement to help you.

DELETE TOP (3) FROM dbo.waqf WHERE ID IN (1,2,3,4,5,6,7,8,9,10)

EDIT

I made a mistake: Using the TOP (n) with an IN clause won't work as you might think - I should have tested it a bit. That will simply delete the first n rows in the table where the ID is in the specified range. Really you should loop through each of your unique IDs and issue a DELETE per id. There's probably a better way of doing this but it's early here and my brain isn't working yet.

DECLARE @id INT
SET @id = 1
WHILE @id <= (SELECT MAX(ID) FROM dbo.waqf)
BEGIN
    DELETE TOP (3) FROM dbo.waqf WHERE ID = @id

    SET @id += 1
END
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top