Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Automated Trigger To Require a WHERE Clause Expand / Collapse
Author
Message
Posted Monday, January 24, 2011 8:36 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 10, 2014 4:59 AM
Points: 127, Visits: 133
Comments posted to this topic are about the item Automated Trigger To Require a WHERE Clause

Rahul
Post #1052832
Posted Monday, January 24, 2011 9:51 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 12:24 AM
Points: 431, Visits: 1,001
Also check my article on same topic.

Thanks


=============================================================
Atif Shehzad
DBA
Zin Technologies
Post #1052846
Posted Monday, January 24, 2011 10:27 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 10, 2014 4:59 AM
Points: 127, Visits: 133
AShehzad (1/24/2011)
Also check my article on same topic.

Thanks


it's nice and ok but will your code be worthless when any table has no primary key and it's always not mandatory to have one .....
so check it out i also did the same mistake but at the later stage i rectified it.........
Thanks


Rahul
Post #1052858
Posted Monday, January 24, 2011 10:35 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 12:24 AM
Points: 431, Visits: 1,001
Yes you are right. I have made the script for tables with clustered index. So for that reason i have also mentioned it through following sentence
One final note before we get started, it is assumed that a cluster index exists for the table on which these triggers would be implemented.

Thanks


=============================================================
Atif Shehzad
DBA
Zin Technologies
Post #1052863
Posted Monday, January 24, 2011 10:42 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 40,385, Visits: 36,827
Hi Rahul

Not sure if it's just a editing omission, but your trigger will only do what you're intending if there are no indexes at all or just a clustered index on the table.

sys.dm_db_partition_stats contains one row per object, per index. So let's say that the trigger's on a table with 20 rows and 3 indexes (one clustered, 2 nonclustered). An update with no where clause is run. @@rowcount will be 20. The sum of rows across sys.dm_db_partition_stats will be 60 however (20 for each index). 20 is not larger than 60 and hence the trigger allows that update.

Fix is simple, your sum from sys.dm_db_partition_stats should be

SELECT SUM(row_count)
FROM sys.dm_db_partition_stats
WHERE OBJECT_ID = OBJECT_ID('tablename')
AND index_id IN (0,1) -- heap and cluster only

Also have you tested this under heavy concurrent load? (inserts running while doing the updates/deletes)?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1052865
Posted Tuesday, January 25, 2011 1:16 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 8:09 AM
Points: 5, Visits: 95
It is a smart workaround, but it doesn't really solve the problem in an elegant way.
I miss a configuration on Sql server that states "Where Clause mandatory". For all tables, for all Sql statements.
I fear every day, that I will forget the where clause eventually and destroy an entire table.
And guess what: Sql doesn't have an Undo button!

But still if you need (and you will need) you can write "Where 1=1", to update/delete every record in a table.
Your workaround treats it like a "missing where".
You can see the difference between an accidentaly forgoten where and one you written on purpose.

I believe this is a huge "security hole" in the Sql language. And yet it stays with us for so many years (and so many tears).

Post #1052909
Posted Tuesday, January 25, 2011 1:22 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 40,385, Visits: 36,827
luciano furlan (1/25/2011)
I fear every day, that I will forget the where clause eventually and destroy an entire table.
And guess what: Sql doesn't have an Undo button!


If you're doing anything on a production server, the first command in your query window and the first you run should be BEGIN TRANSACTION. That way if you do accidentally leave out (or not select) the where clause, you can roll it back.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1052910
Posted Tuesday, January 25, 2011 1:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:01 AM
Points: 163, Visits: 249
Lol, I know I have done this twice in my life. 1st time was on the dev box on a restore of production, so I could simply run a update query reading the correct values from the production database. Second time was a week or 2 ago on a system i developed freelance. There was no source, and no backups as yet. It was an update of the product code of only (well suposed to be...) two products. Lucky for me the product code is based on the primary key, the store branch, and category. So I just ran a update query to reconstruct the original product item no.
This solution might not be elegant, but will save paper in the sense of less written warning will be be issued to slightly careless developers/dba's

From my side, Thank you.
Post #1052912
Posted Tuesday, January 25, 2011 2:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 14, 2011 12:49 AM
Points: 2, Visits: 5
Thanks
Post #1052925
Posted Tuesday, January 25, 2011 2:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 25, 2011 3:36 PM
Points: 3, Visits: 5
Hello,
just to finish undestanding, I would like to know in which cases @@rowcount can be greater than the number of rows. If anybody could explain I would appreciate.
Kind regards
Post #1052938
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse