有時候需要刪除所有預存程式,但是卻一個一個刪除卻是很耗時間的,該如何一次刪除完成呢?
其實作法就是從資料庫的sys.Objects資料表中找出type為P的,這些就是預存,然後透過cursor把符合名稱的預存程式給Drop掉:
declare @procName varchar(500)
declare cur cursor
for select [name] from sys.objects where type = 'p'
open cur
fetch next from cur into @procName
while @@fetch_status = 0
begin
exec('drop procedure ' + @procName)
fetch next from cur into @procName
end
close cur
deallocate cur
注意這是個危險動作,請確認無誤後再執行。
留言