Recently, I had a situation where I needed to delete several rows from a table and write the column name incorrectly. The error was not reset, and ALL rows were deleted from the table. Here is a script that reproduces the problem.
There is an order table with four orders with OrderID. There is a table LIST_TO_DELETE with one ItemID.
I had to use
DELETE TOP(1) FROM
instead i used
DELETE TOP(1) FROM
This led to deleting all the rows in the #Orders table when I only wanted to delete it.
CREATE TABLE
INSERT INTO
CREATE TABLE
DECLARE @rowcount INT = 1
WHILE @rowcount > 0
BEGIN
DELETE TOP(1) FROM
SET @rowcount = @@rowcount
END
SELECT * FROM
DROP TABLE
DROP TABLE
In my source code, the order table was real, and LIST_TO_DELETE was a table variable, but it doesn't seem to matter what type of table is used. If I use any column name except OrderID or ItemID, I get an error
DELETE TOP(1) FROM
Invalid column name 'OtherID'
?