Technical Article

Correction to "drop/recreate objects" script.

,

Regarding the recent script I submitted (dropping/recreating all procedures/views) - I made an important oversight.

I neglected to add a CASE statement in order to make sure that the appropriate type of object was being referenced in the DROP statement.

Below is the corrected script:

ALTER PROCEDURE drop_and_recreate_all 

/*
Generates a script for dropping all views or procedures   in a database.
*/
@type char(1) = NULL

/*
@type values:
'p' = stored procedure
'v' = view
'if' = inline table-valued function
'fn' = 'scalar function'
*/
as

set nocount on

select 'drop ' + 
case @type 
when 'p' then 'procedure ' 
when 'v' then 'view '
when 'fn' then 'function ' 
when 'if' then 'function ' 
end
+ name + char(10) + 'go' + char(10) + 
text + char(10) + 'go' + char(10) + char(10)
from sysobjects o
join syscomments c
on o.id = c.id
where o.type = @type
and OBJECTPROPERTY(o.id, 'IsMSShipped') = 0

/*
Last line is necessary in order to ignore system objects
*/

Rate

5 (1)

Share

Share

Rate

5 (1)