I'm new to the Oracle database system. What is the equivalent of the below SQL statement in Oracle 12c?
DROP VIEW IF EXIST <view_name>
I'm new to the Oracle database system. What is the equivalent of the below SQL statement in Oracle 12c?
DROP VIEW IF EXIST <view_name>
Except from simply calling DROP VIEW
, you can write a procedure that checks before:
BEGIN FOR i IN (SELECT null FROM user_views WHERE view_name = 'BOOKS_emp') LOOP EXECUTE IMMEDIATE 'DROP VIEW books_emp'; END LOOP; END; /
What you've written is working in 12c without any issue. Also, you can simply use,
DROP VIEW <VIEW_NAME>;
You can do something via a procedure, but your query is irrelevant when you can run drop view viewname. It can straightaway show you if it exists or not.
Create or Replace Procedure
dropifexists(viewname IN
varchar(50))
AS
Declare
flag number(10);
Begin
SELECT count(*) into flag FROM
user_views
WHERE view_name = viewname;
If(flag>0)
Drop View viewname;
Else
dbms_output.putline('View already
exists')
End if;
End;
/
And, once created, you can call the same procedure:
begin
dropifexists('viewname' ) ;
end;
/
Are you sure you want to replace this functionality 1:1? Normally, you'd do a DROP VIEW IF EXISTS
in SQL Server because until recently, SQL Server didn't have a CREATE OR ALTER VIEW
option. So your promotion scripts would drop the view if it existed and then have a CREATE VIEW
statement so it could be run multiple times without a problem.
Oracle has always (well, at least the past couple of decades) supported a CREATE OR REPLACE VIEW
statement, so your Oracle scripts would normally just contain a CREATE OR REPLACE VIEW statement. There wouldn't normally be any need to try to drop the view first.
You'd only want to drop the view if it was an obsolete object that you wanted to be removed permanently, but in that case you'd know that the view exists, so it wouldn't make a lot of sense to check for existence first.