Solved

Oracle create table based on multiple conditions

Posted on 2011-02-21
4
397 Views
Last Modified: 2012-05-11
Hi,

I'm trying to figure out the best approach to creating an oracle table If a certain condition is true.

for example.
Create table1 if name <> ' ' and address <> ' '
Create table1 if name <> ' ' and address <> ' ' and phone = ' '

Should a case statement be used in this scenario?
0
Comment
Question by:phoen08
4 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
you can't do conditional creates within a single statement.
you'll have to check your conditions first then issue whatever create is appropriate.

what is the intent here?  It's not standard (and certainly not recommended) practice to create tables on the fly in Oracle.

Are you trying to create "temp" tables to work with?  If so, doing it dynamically is the wrong approach
0
 
LVL 15

Expert Comment

by:Devinder Singh Virdi
Comment Utility
Are you looking something like this?

File_1.sql
Set Serveroutput on
set echo off
.
.
.
spool &1
begin
  if name <> '' and address <> '' then
    DBMS_OUTPUT.PUT_LINE('CREATE TABLE(col1 varchar2(2));');
  end if;

  if name <> '' and address <> '' then
    DBMS_OUTPUT.PUT_LINE('CREATE TABLE (col1 varchar2(2), col2 varchar(2)); ');
  end if;
end;
spool off;

sqlplus -s username/password @File_1.sql my_spool.sql
sqlplus -s username/password @my_spool.sql
0
 

Author Comment

by:phoen08
Comment Utility
I'm trying to convert an old process that would export output to a text file to a process that would store the data in oracle tables. One of the sections in the file process outputs columns based on whatever condition is presents.  After further review, I believe this step in the process is only changing the column values based on the condition.  So, I may just need multiple case statements to change the column data based on the condition.  There would only be one table created from all of the condition statements.
0
 
LVL 47

Accepted Solution

by:
schwertner earned 500 total points
Comment Utility
Dynamic SQL could be an answer:

.......
v_sql   VARCHAR2(900);
.......

IF  name <> ' ' and address <> ' '  THEN v_sql := '  Create table1 (col1 VARCHAR2(80), .....)';
ELSIF  name = ' some' and address = ' another' and phone = '906377 ' THEN v_sql := '  Create table2 (col1 NUMBER,.....)';
END IF;

execute immediate v_sql;

....

EXCEPTION
   WHEN OTHERS THEN
   .....




0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now