An example of whether an Oracle judgment table exists

Source: Internet
Author: User
Tags table name

The main is Query All_tables table table_name and owner, if the table exists, execute execute immediate ' drop table
TABLE_NAME ';

--Determine if the table exists, delete if present

Declare
Num number;
Begin
Select COUNT (1) into NUM from all_tables where table_name = ' EMP ' and owner= ' SCOTT ';
If Num=1 Then
Execute immediate ' drop table EMP ';
End If;
End

/
--Create a table

CREATE TABLE EMP
(EMPNO number (4) is not NULL,
Ename VARCHAR2 (10),
JOB VARCHAR2 (9),
MGR Number (4),
HireDate DATE,
SAL Number (7, 2),
COMM Number (7, 2),
DEPTNO number (2));

The above is to create the table below let's see if a table name exists before deleting the table and if so, delete the example.

Deleting a nonexistent table in Oracle, such as drop table notexisttable, prompts:

ORA-00942: Table or view does not exist,

If you use a program to execute the statement, you will report an exception, which requires us to determine whether the table exists before we delete it, or delete it if it exists.

The following is SQL that deletes a table without using a stored procedure implementation:

DECLARE num number;
BEGIN
SELECT COUNT (1) into the NUM from user_tables WHERE table_name = UPPER (' tablename ');
IF num > 0 THEN
EXECUTE IMMEDIATE ' DROP TABLE tablename ';
End IF;
End;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.