|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:32 AM
Points: 127,
Visits: 122
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 4:30 AM
Points: 419,
Visits: 738
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:32 AM
Points: 127,
Visits: 122
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 4:30 AM
Points: 419,
Visits: 738
|
|
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 PRAL
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 12:38 AM
Points: 37,725,
Visits: 29,981
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, March 14, 2013 10:50 AM
Points: 5,
Visits: 82
|
|
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).
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 12:38 AM
Points: 37,725,
Visits: 29,981
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, September 18, 2012 1:28 AM
Points: 161,
Visits: 233
|
|
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.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, February 14, 2011 12:49 AM
Points: 2,
Visits: 5
|
|
Thanks
|
|
|
|
|
Forum 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
|
|
|
|