Tuesday, June 5, 2012

How to stop bulk update in sql server

I was searching how to prevent SQL Injection and stop updating all rows of the table and I got a solution in given link http://www.mssqltips.com/sqlservertip/1851/prevent-accidental-update-or-delete-commands-of-all-rows-in-a-sql-server-table/

Post is really good and helpfull, But when I was Creating Trigger to stop bulk update by following the link I found index =1 is not working for few tables then i used index=0 still not for all tables finally I make some changes in SQL and got the result.

I removed Index from where clause and finally its work for all tables.

My Modified Query is given below.

USE AdventureWorks
GO
CREATE TRIGGER [Purchasing].[uPreventWholeUpdate] 
ON [Purchasing].[VendorContact] 
FOR UPDATE AS 
BEGIN
     DECLARE @Count int
     SET @Count = @@ROWCOUNT;
         
     IF @Count >= (SELECT SUM(row_count)
         FROM sys.dm_db_partition_stats 
         WHERE OBJECT_ID = OBJECT_ID('Purchasing.VendorContact' ) )
     BEGIN
         RAISERROR('Cannot update all rows',16,1) 
         ROLLBACK TRANSACTION
         RETURN;
     END
END
GO
 
 
If this link is help full please do post comments.

No comments: