In my current job, I have to run SQL script once in a while to update my database. Mostly they are to update certain records that did not work properly.There is a page functionality to do the same but that is a bit longer route so I do it directly against the database.
I just ran an update query and accidentally updated all of my 30,000+ records. Luckily I was working in my test database.
//Particular query that I am talking about is this
update customers set
customer_id = 100 // where clause is missing and it will update all records
-
I was wondering, Is there a way to protect the database against such bulk update, may be a trigger that will half if update is affecting more than 500 records? Is there any way to protect database from accidental ill formatted queries. I am particularly interested in updates though.
-
Am I doing something really wrong when I use SQL Script directly against my production server. I am new and I need expert advice.
Looking at your query I assume that your Customers table doesn’t have a Primary Key with Identity turned on for the Customer_Id, having both these set on the table would certainly have stopped that script from completing.
I would suggest that you wrap your query in a transaction. Initially running the script with a rollback statement testing the output before you call Rollback. If the results are as you expected, replace the rollback with a commit and re-run the query.
EXAMPLE