Solved

Oracle create table based on multiple conditions

Posted on 2011-02-21
4
398 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
ID: 34945643
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
ID: 34946746
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
ID: 34950691
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
ID: 34950993
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

910 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

22 Experts available now in Live!

Get 1:1 Help Now