我有很長的腳本從表中的大量(超過100+)刪除某些特定的數據,它看起來像這樣更好的辦法:處理許多delete語句
...
Delete from Table_name WHERE Company not in ('Company1','Company2')
Delete from Table_name WHERE Company not in ('Company1','Company2')
Delete from Table_name WHERE Company not in ('Company1','Company2')
Delete from Table_name WHERE Company not in ('Company1','Company2')
...
我想改變這個,所以我不必改變每一行的變量,我希望能夠在開始時設置where語句,並且這將改變所有的刪除行
declare .....something something
SELECT CompanyID
FROM _Company
WHERE Company in ('Company1','Company2') -- I want to change this where statement only
Delete from Table_name WHERE Company not in (variable)
Delete from Table_name WHERE Company not in (variable)
Delete from Table_name WHERE Company not in (variable)
Delete from Table_name WHERE Company not in (variable)
請記住,如果單個事務嘗試從任何給定表中刪除超過5000行,SQL Server將執行**鎖升級**。這會在該表上放置一個**獨佔鎖**,甚至在該事務被提交(或回滾)之前阻止從該表中選擇「SELECT」。儘量避免一次刪除超過5000行 –