on 2010 Feb 23 10:02 PM
If this sample was helpful to you, vote it up.
If you have an improved version, or a closely related sample, post it as an answer.
If you have a question or complaint, post it as a comment (or an answer, if you have a lot to say 🙂
Description:
Know the view you want to drop, but don't know how to check for existence? Want to drop all views in the system, but have no automated way of doing it? Specify the view name as the parameter to drop only that view (IF it exists). Leave it empty to drop'em all.
Usage:
To drop a single view by name:
call sp_Drop_View('vw_People_Info');
To drop all views in a database:
call sp_Drop_View();
Code:
create procedure sp_Drop_View (IN @viewName varchar(128) DEFAULT 'NONE')
begin
IF @viewName = 'NONE' THEN
FOR VIEW_NAME_FOR as VIEW_NAME_CURSOR dynamic scroll cursor for
select st.table_name as VIEW_NAME
from systab st
where st.creator = 1 AND
st.table_type = 'VIEW'
order by st.table_name asc do
execute immediate 'DROP VIEW ' || VIEW_NAME;
END FOR;
ELSE
if exists(select 1 from sys.systable where table_name=@viewName and table_type='VIEW') then
EXECUTE IMMEDIATE 'DROP VIEW ' || @viewName;
end if;
END IF;
end;
Request clarification before answering.
I have used a similar proc in the past, but fortunately SA 11.0.1 introduced the following handy syntax:
DROP VIEW IF EXISTS [owner].viewname
(As it does for other database objects like triggers etc.)
But of course it does not allow to drop all views:)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
39 | |
15 | |
9 | |
7 | |
5 | |
5 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.