0 | i have to delete a large amount of data. truncate is not possible because of relations. And I don't wanna drop the table because of views. I am using code below but is there better idea?
| ||||||||
|
Truncate table would be the best by far. If you're worried about relationships because of the referencial integrity, than I suggest you update separately those tables that reference it by foreign key (e.g. if foreign keys have ON DELETE SET NULL then UPDATE RefTbl SET Key = NULL WHERE Key IS NOT NULL , similarly, delete if cascade etc...).Alternatively, if you don't want that, you can alter your query to be a bit quicker by using ROWCOUNT:
Also, I'm not sure why are you doing this inside a transaction? It would create a huge transaction log and impact your performance severely... When you're performing stuff like dumping table content usually transactions are not relevant (it could be relevant in your case, but I'm just saying it's rarely the case). If you do need transaction, than at least move your delay out of it... | |||||
You could do it a bit simpler:
|